我们有一个任务是编译一个lex和一个yacc praser代码,然后使用cc tab.yc -ll -Ly命令一起运行它们当我们分开它们时它们编译得很好但是编译两个部分作为一个给出10错误线。
第一部分是Lex Code:
%option yylineno
%pointer
%{
#include <stdlib.h>
#include <string.h>
void yyerror(const char *);
%}
low \_
identifier {letters}{digit}*{low}{letters}|{letters}
stringERR {doubleQuotes}{doubleQuotes}+|{doubleQuotes}
charERR {singleQuotes}+{digits}*{letters}*{singleQuotes}+
ERR {charERR}|{stringERR}
type boolean|string|char|integer|intptr|charptr|var
dbland "&&"
devide "/"
assign "="
equal "=="
greater ">"
lesser "<"
greaterequal ">="
lesserequal "<="
minus "-"
plus "+"
not "!"
notequal "!="
or "||"
multiply "*"
power "^"
AND "&"
literBool true|false
letter [a-z]|[A-Z]
letters {letter}+
singleQuotes '
literChar {singleQuotes}{letter}{singleQuotes}
digit [0-9]
digitZero 0
octalDigit [1-7]
octal {digitZero}{octalDigit}{digitZero}*{octalDigit}*
digits {digit}+
digitNoZero[1-9]
decimal {digit}|{digitNoZero}{digits}
hexLetter A|B|C|D|E|F
hex 0(x|X){digit}+{hexLetter}*|0(x|X){digit}*{hexLetter}+
letterB b
digitOne 1
binaryInt ({digitZero}|{digitOne})+{letterB}
integer {binaryInt}|{hex}|{octal}|{decimal}
doubleQuotes \"
ltrlString {doubleQuotes}{letters}*{decimal}*{hex}*{octal}*{binaryInt}*{dbland}*{devide}*{assign}*{equal}*{greater}*{lesser}*{greaterequal}*{lesserequal}*{minus}*{plus}*{not}*{notequal}*{or}*{multiply}*{AND}*{power}*{doubleQuotes}
comment {backslash}{parcent}{space}*({letters}*{space}*{identifier}*{space}*{decimal}*{space}*{hex}*{space}*{octal}*{space}*{binaryInt}*{space}*{dbland}*{devide}*{assign}*{equal}*{greater}*{lesser}*{greaterequal}*{lesserequal}*{minus}*{$nus}*{plus}*{not}*{notequal}*{or}*{multiply}*{AND}*{power}*{ltrlString}*)*{space}{parcent}{backslash}
colon ":"
openSq "["
closeSq "]"
semicolon ";"
parcent "%"
space " "
comma ","
backslash "/"
clos ")"
opn "("
charptr charptr
pointer {colon}{space}{charptr}|"="{space}"&"{identifier}
pointerErr "&"{identifier}|{charptr}
ELSE "else"{space}*
statif "if"{space}*
whileLoop "while"{space}*
returnState "return"{space}*
func "procedure"{space}*
%%
{dbland} return dbland;
{devide} return devide;
{assign} return assign;
{equal} return equal;
{greater} return greater;
{lesser} return lesser;
{greaterequal} return greaterequal;
{lesserequal} return lesserequal;
{minus} return minus;
{plus} return plus;
{not} return not;
{notequal} return notequal;
{or} return or;
{multiply} return multiply;
{power} return power;
{AND} return AND;
{literBool} return literBool;
{literChar} return literChar;
{decimal} return decimal;
{hex} return hex;
{octal} return octal;
{binaryInt} return binaryInt;
{ltrlString} return ltrlString
{type} return type;
{identifier} return identifier;
{ERR} return ERR;
{comment} return comment;
{pointer} return pointer;
{pointerErr} return pointerErr;
{statif} return statif;
{ELSE} return ELSE;
{whileLoop} return whileLoop;
{returnState} return returnState;
{func} return func;
{semicolon} return semicolon;
{comma} return comma;
[\*\(\)\.\+\-\%] { return *yytext; }
[0-9][0-9]* { return 'n'; }
[ \t\n] ; /* skip whitespace */
%%
int yywrap(void) {
return 1;
}
yacc代码:
%token low identifier stringERR charERR ERR type operator literBool letter
%token dbland literChar decimal hex octal integer
%token binaryInt ltrString comment pointer pointerErr
%token statif ELSE whileLoop returnState func comma semicolon
%token EOL LPAREN RPAREN UMINUS
%left equal greater notequal lesser greaterequal lesserequal
%left '|' %left '&' %left SHIFT /* << >> */
%left minus plus
%left multiply devide '%' MOD %left power
%left not or AND comma
%nonassoc UMINUS
%%
s: BLOCK;
BLOCK: expr|logicOp|varible_declaration|ifExp|whileExp|procExp|semicolon;
expr: exp{printtree($1);}
exp:
identifier {$$=mknode(yytext,NULL,NULL);}
| LPAREN expr RPAREN {$$=$2;}
| exp plus exp {$$= mknode("+" $1,$3);}
| exp minus exp {$$= mknode("-" $1, $3);}
| exp multiply exp {$$=mknode("*" $1, $3);}
| exp devide exp {$$=mknode("/" $1, $3);}
| "-" exp %prec UMINUS {-$2}
varible_declaration: var{printtree($1);}
var : "VAR" identifier_list ":" typet ";" {$$ = mknode("var", $2, $4);}
typet:
integer{$$ = mknode(yytext,NULL,NULL);}
|binaryInt {$$ = mknode(yytext,NULL,NULL);}
|type {$$ = mknode(yytext,NULL,NULL);}
identifier_list: identifier_list comma identifier_list
{$$= mknode(",",$1, $3);}
|identifier {$$ = mknode(yytext,NULL,NULL);}
logicOp: op{printtree($1);}
op:exp equal exp {$$ = mknode("==",$1,$3);}
|exp notequal exp {$$ = mknode("!=",$1,$3);}
|exp or exp {$$ = mknode("||",$1,$3);}
|exp AND exp {$$ = mknode("&&",$1,$3);}
|exp greater exp {$$ = mknode(">",$1,$3);}
|exp greaterequal exp {$$ = mknode(">=",$1,$3);}
|exp lesser exp {$$ = mknode("<",$1,$3);}
|exp lesserequal exp {$$ = mknode("<=",$1,$3);}
ifExp: if{printtree($1);}
if:statif '(' logicOp ')' '{' BLOCK '}' ELSE '{' BLOCK '}' {$$ = mknode("if",$3,mknode("else",$6,$10));}
|statif '(' logicOp ')' '{' BLOCK '}' {$$=mknode("if",$3,$6);}
whileExp: while{printtree($1)}
while:whileLoop '(' logicOp ')' '{' BLOCK '}' {$$=mknode("while",$3,$6);}
procExp: proc{printtree($1)}
proc:func identifier '(' identifier_list ')' returnState type '{' BLOCK '}' {$$ = mknode("procedure",$2,"TODO");}
%%
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yylex(void);
void yyerror(const char *);
tyepdef struct node{
char * token;
struct node *left;
struct node *right;
};
node * mknode(char * token , node * left,node * right);
void printtree(node * tree);
#define yySType struct node *
#include "lex.yy.c"
main()
{ return yyparse(); }
nose * mknode(char * token,node * left, node * right)
{
node * newnode = (node*)malloc(sizeof(node));
char 8 newstr = (char*)malloc(sizeof(token)+1);
strcpy("newstr,token");
newnode->left=left;
newnode->right=right;
newnode->token=newstr;
}return newnode;
void printtree(node * tree)
{
printf("%s\n",tree->token);
if (tree->left) printtree(tree->left);
if (tree->right) printtree(tree->left);
}
extern int yylineno;
void yyerror(const char *s)
{
fprintf(stderr, "%s at line %d\n", s, yylineno);
return;
}
我们得到的错误如下:
[tzurisa@Ac-Aix backup]$ nano test.l
[tzurisa@Ac-Aix backup]$ lex test.l
[tzurisa@Ac-Aix backup]$ yacc test.y
[tzurisa@Ac-Aix backup]$ cc -o test y.tab.c -ll -Ly
test.y:63: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
test.y:68: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
test.y:69: error: expected ‘)’ before ‘*’ token
In file included from test.y:72:
test.l: In function ‘yylex’:
test.l:74: error: ‘assign’ undeclared (first use in this function)
test.l:74: error: (Each undeclared identifier is reported only once
test.l:74: error: for each function it appears in.)
In file included from test.y:72:
test.l:94: error: ‘ltrlString’ undeclared (first use in this function)
test.l:95: error: expected ‘;’ before ‘break’
test.y: At top level:
test.y:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
test.y:83: error: expected identifier or ‘(’ before ‘return’
test.y:84: error: expected ‘)’ before ‘*’ token
test.y: In function ‘yyparse’:
test.y:20: error: expected ‘)’ before ‘yyvsp’
test.y:21: error: expected ‘)’ before ‘yyvsp’
test.y:22: error: expected ‘)’ before ‘yyvsp’
test.y:23: error: expected ‘)’ before ‘yyvsp’
test.y:24: error: expected ‘;’ before ‘}’ token
test.y:51: error: expected ‘;’ before ‘}’ token
test.y:53: error: expected ‘;’ before ‘}’ token
会向任何可以告诉我们这里错误的人提供帮助我们已经尝试了很多我们仍然会遇到这些错误的事情。
答案 0 :(得分:1)
如果您查看test.y
的第63行,如第一条错误消息所示,您将看到第一个问题;你拼错了typedef
。修复此问题,然后通过查看指示的行来检查剩余的错误(如果有)。