English 中文(简体)
灵活性/毒性,错误:未申报
原标题:Flex/bison, error: undeclared

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();
}
问题回答

你的首要问题是,你试图将你的灵活做法纳入你的教区。 您(至少通常)想要做的是,通过使用<代码>yacc-d,(如果你必须)生产一台头盔(y.tab.h),并将之列入你的弹性。

备选案文1:

%{
#include "y.tab.h"
int yylinenu= 1;
int yycolno= 1;
%}
// ...

既然贵族提到上述变量,那么你就会宣布,而不是在贵族来源档案中:

extern int yylinenu;
extern int yycolno;

www.un.org/Depts/DGACM/index_french.htm 你们或许会坐在试图弄清正在做的事情。

void yyerror(char *msg)
{
    printf("Line: %d , Column: %d : %s 
", yylinenu, yycolno, msg); 
}

int main(int argc, char *argv[])
{
    return yyparse();
}

除此以外,你的克文法有细微细节或两点,即我确确实是你所期望的。 例如:

|WHILE LINKEKLAMMER Cond RECHTEKLAMMER Stmt 
    {puts("		Stmt :  PRINTF  Expr  ; ");}
|PRINTF Expr SEMIKOLON  
    {puts("		Stmt :  PRINTF  Expr  ; ");}

简言之,在你与“同时”相匹配的地方,你想要印刷“但”而不是“印本”:

|WHILE LINKEKLAMMER Cond RECHTEKLAMMER Stmt 
    {puts("		Stmt :  WHILE  Expr  ; ");}

同样:

|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt    
    {puts("		Stmt :  (  Cond  )  Stmt");}
|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt  ELSE Stmt 
    {puts("		Stmt :  (  Cond  )  Stmt  ELSE  Stmt");}

我猜想,如果在开始,你可能想印刷:

|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt    
    {puts("		Stmt :  IF   (  Cond  )  Stmt");}
|IF LINKEKLAMMER Cond RECHTEKLAMMER Stmt  ELSE Stmt 
    {puts("		Stmt :  IF   (  Cond  )  Stmt  ELSE  Stmt");}

作为最后一点,我建议说几句 in语和空白,这样,你的克文法规则就这样规定:

Term:Factor                 {puts("		Term : Factor");}
    | Factor MAL Term       {puts("		Term : Factor  *  Term");}
    | Factor SLASH Term     {puts("		Term : Factor  /  Term");}
    ;

Factor:SimpleExpr           {puts("		Factor : SimpleExpr");}
    | MINUS SimpleExpr      {puts("		Factor :  -  SimpleExpr");}
    ;

当然,你可以有所不同,例如将行动分开(特别是如果行动持续了很长时间),但总的想法仍然不变。 您确实需要评论,以说明某一规则何时结束,另一开始——格式安排可以表明这一点。

Edit:我只想提及另一个观点:如果是自下而上的教区(如 bis/acc/by/by),通常宁愿再入侵,因此,你一般倾向于改变:

Stmts : Stmt                {puts("		Stmts : Stmt");}
    | Stmt Stmts            {puts("		Stmts : Stmt Stmts");}
    ;

:

Stmts : Stmt                {puts("		Stmts : Stmt");}
    | Stmts Stmt            {puts("		Stmts : Stmts Stmt");}
    ;

举例来说,联合管理计划指示贴在标签L1上。

:L1
IF FLAG AND X"0001"
EVT 23;
ELSE
WAIT 500 ms;
JMP L1;
END IF;

感谢。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签