hallo, i have a problem, the followed program gives back an error, error:: Undeclared(first use in function), why this error appears all tokens are declared, but this error comes, can anyone help me, here are the lex and yac files.thanks
lex:
%{
int yylinenu= 1;
int yycolno= 1;
%}
%x STR
DIGIT [0-9]
ALPHA [a-zA-Z]
ID {ALPHA}(_?({ALPHA}|{DIGIT}))*_?
GROUPED_NUMBER ({DIGIT}{1,3})(.{DIGIT}{3})*
SIMPLE_NUMBER {DIGIT}+
NUMMER {GROUPED_NUMBER}|{SIMPLE_NUMBER}
%%
<INITIAL>{
[
] {++yylinenu ; yycolno=1;}
[ ]+ {yycolno=yycolno+yyleng;}
[ ]+ {yycolno=yycolno+(yyleng*8);}
"*" {return MAL;}
"+" {return PLUS;}
"-" {return MINUS;}
"/" {return SLASH;}
"(" {return LINKEKLAMMER;}
")" {return RECHTEKLAMMER;}
"{" {return LINKEGESCHWEIFTEKLAMMER;}
"}" {return RECHTEGESCHEIFTEKLAMMER;}
"=" {return GLEICH;}
"==" {return GLEICHVERGLEICH;}
"!=" {return UNGLEICH;}
"<" {return KLEINER;}
">" {return GROSSER;}
"<=" {return KLEINERGLEICH;}
">=" {return GROSSERGLEICH;}
"while" {return WHILE;}
"if" {return IF;}
"else" {return ELSE;}
"printf" {return PRINTF;}
";" {return SEMIKOLON;}
//[^
]* { ;}
{NUMMER} {return NUMBER;}
{ID} {return IDENTIFIER;}
" {BEGIN(STR);}
. {;}
}
<STR>{
{++yylinenu ;yycolno=1;}
([^"\]|"\t"|"\n"|"\r"|"\b"|"\"")+ {return STRING;}
" {BEGIN(INITIAL);}
}
%%
yywrap()
{
}
YACC:
%{
#include stdio.h>
#include string.h>
#include "lex.yy.c"
void yyerror(char *err);
int error=0,linecnt=1;
%}
%token IDENTIFIER NUMBER STRING COMMENT PLUS MINUS MAL SLASH LINKEKLAMMER RECHTEKLAMMER LINKEGESCHWEIFTEKLAMMER RECHTEGESCHEIFTEKLAMMER GLEICH GLEICHVERGLEICH UNGLEICH GROSSER KLEINER GROSSERGLEICH KLEINERGLEICH IF ELSE WHILE PRINTF SEMIKOLON
%start Stmts
%%
Stmts : Stmt
{puts(" Stmts : Stmt");}
|Stmt Stmts
{puts(" Stmts : Stmt Stmts");}
; //NEUE REGEL----------------------------------------------
Stmt : LINKEGESCHWEIFTEKLAMMER Stmts RECHTEGESCHEIFTEKLAMMER
{puts(" Stmt : { Stmts } ");}
|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt
{puts(" Stmt : ( Cond ) Stmt");}
|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt ELSE Stmt
{puts(" Stmt : ( Cond ) Stmt ELSE Stmt");}
|WHILE LINKEKLAMMER Cond RECHTEKLAMMER Stmt
{puts(" Stmt : PRINTF Expr ; ");}
|PRINTF Expr SEMIKOLON
{puts(" Stmt : PRINTF Expr ; ");}
|IDENTIFIER GLEICH Expr SEMIKOLON
{puts(" Stmt : IDENTIFIER = Expr ; ");}
|SEMIKOLON
{puts(" Stmt : ; ");}
;//NEUE REGEL ---------------------------------------------
Cond: Expr GLEICHVERGLEICH Expr
{puts(" Cond : == Expr");}
|Expr UNGLEICH Expr
{puts(" Cond : != Expr");}
|Expr KLEINER Expr
{puts(" Cond : < Expr");}
|Expr KLEINERGLEICH Expr
{puts(" Cond : <= Expr");}
|Expr GROSSER Expr
{puts(" Cond : > Expr");}
|Expr GROSSERGLEICH Expr
{puts(" Cond : >= Expr");}
;//NEUE REGEL --------------------------------------------
Expr:Term
{puts(" Expr : Term");}
|Term PLUS Expr
{puts(" Expr : Term + Expr");}
|Term MINUS Expr
{puts(" Expr : Term - Expr");}
;//NEUE REGEL --------------------------------------------
Term:Factor
{puts(" Term : Factor");}
|Factor MAL Term
{puts(" Term : Factor * Term");}
|Factor SLASH Term
{puts(" Term : Factor / Term");}
;//NEUE REGEL --------------------------------------------
Factor:SimpleExpr
{puts(" Factor : SimpleExpr");}
|MINUS SimpleExpr
{puts(" Factor : - SimpleExpr");}
;//NEUE REGEL --------------------------------------------
SimpleExpr:LINKEKLAMMER Expr RECHTEKLAMMER
{puts(" SimpleExpr : ( Expr ) ");}
|IDENTIFIER
{puts(" SimpleExpr : IDENTIFIER ");}
|NUMBER
{puts(" SimpleExpr : NUMBER ");}
|STRING
{puts(" SimpleExpr : String ");}
;//ENDE -------------------------------------------------
%%
void yyerror(char *msg)
{
error=1;
printf("Line: %d , Column: %d : %s
", yylinenu, yycolno,yytext, msg);
}
int main(int argc, char *argv[])
{
int val;
while(yylex())
{
printf("
",yytext);
}
return yyparse();
}