Flex/bison, error: undeclared

Posted by Imran on Stack Overflow See other posts from Stack Overflow or by Imran
Published on 2010-04-19T10:06:56Z Indexed on 2010/04/20 15:53 UTC
Read the original article Hit count: 212

Filed under:
|
|
|
|

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>{
[\n]                      {++yylinenu ; yycolno=1;} 
[ ]+                      {yycolno=yycolno+yyleng;} 
[\t]+             {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;}  
\/\/[^\n]*                { ;}
{NUMMER}                  {return NUMBER;}
{ID}                      {return IDENTIFIER;}
\"                {BEGIN(STR);}                  
.                         {;} 
}

<STR>{ 
\n                        {++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("\t\tStmts : Stmt");}
|Stmt Stmts
{puts("\t\tStmts : Stmt Stmts");}
; //NEUE REGEL----------------------------------------------
Stmt : LINKEGESCHWEIFTEKLAMMER Stmts RECHTEGESCHEIFTEKLAMMER
{puts("\t\tStmt : '{' Stmts '}'");}
|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt
{puts("\t\tStmt : '(' Cond ')' Stmt");}
|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt  ELSE Stmt
{puts("\t\tStmt : '(' Cond ')' Stmt 'ELSE' Stmt");}
|WHILE LINKEKLAMMER Cond RECHTEKLAMMER Stmt
{puts("\t\tStmt : 'PRINTF' Expr ';'");}
|PRINTF Expr SEMIKOLON
{puts("\t\tStmt : 'PRINTF' Expr ';'");}
|IDENTIFIER GLEICH Expr SEMIKOLON
{puts("\t\tStmt : 'IDENTIFIER' '=' Expr ';'");}
|SEMIKOLON
{puts("\t\tStmt : ';'");}
;//NEUE REGEL ---------------------------------------------
Cond: Expr GLEICHVERGLEICH Expr 
{puts("\t\tCond : '==' Expr");}
|Expr UNGLEICH Expr
{puts("\t\tCond : '!=' Expr");}
|Expr KLEINER Expr
{puts("\t\tCond : '<' Expr");}
|Expr KLEINERGLEICH Expr
{puts("\t\tCond : '<=' Expr");}
|Expr GROSSER Expr
{puts("\t\tCond : '>' Expr");}
|Expr GROSSERGLEICH Expr
{puts("\t\tCond : '>=' Expr");}
;//NEUE REGEL --------------------------------------------
Expr:Term 
{puts("\t\tExpr : Term");}
|Term PLUS Expr 
{puts("\t\tExpr : Term '+' Expr");}
|Term MINUS Expr 
{puts("\t\tExpr : Term '-' Expr");}
;//NEUE REGEL --------------------------------------------
Term:Factor
{puts("\t\tTerm : Factor");}
|Factor MAL Term
{puts("\t\tTerm : Factor '*' Term");}
|Factor SLASH Term
{puts("\t\tTerm : Factor '/' Term");}
;//NEUE REGEL --------------------------------------------
Factor:SimpleExpr
{puts("\t\tFactor : SimpleExpr");}
|MINUS SimpleExpr
{puts("\t\tFactor : '-' SimpleExpr");}
;//NEUE REGEL --------------------------------------------
SimpleExpr:LINKEKLAMMER Expr RECHTEKLAMMER
{puts("\t\tSimpleExpr : '(' Expr ')'");}
|IDENTIFIER
{puts("\t\tSimpleExpr : 'IDENTIFIER'");}
|NUMBER 
{puts("\t\tSimpleExpr : 'NUMBER'");}
|STRING
{puts("\t\tSimpleExpr : 'String'");}
;//ENDE -------------------------------------------------
%%
void yyerror(char *msg)
{
 error=1;
 printf("Line: %d , Column: %d : %s \n", yylinenu, yycolno,yytext, msg); 
}
int main(int argc, char *argv[])
{

        int val;
        while(yylex())  
        {       
         printf("\n",yytext);       
    }
    return yyparse();
}

© Stack Overflow or respective owner

Related posts about yacc

Related posts about bison