错误:“期望的标识符或'(''''''''''''''''''''''''''

时间:2016-12-24 16:50:54

标签: yacc lex

我试图为Csimple构建一个编译器。当我运行命令 “cc -o test y.tab.c -ll -L.y”我遇到了一道错误。 我很茫然。 我知道我的yacc文件解析得不好, 但首先我需要它运行所以我可以看到输出。 yacc文件中间的行“%{”出现错误。

法:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

YACC:

%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}*{p$us}*{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;
{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;
}

执行:

%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>
    #define YYDEBUG 1
    int yylex(void);
    void yyerror(const char *);
    tyepdef struct node{char * token;
    struct node *left;
    struct  node *right;
    }node;
    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;
}

这就是故障发生的地方。

正如我所说,我得到了很多错误,但第一个错误 test.y:60:错误:预期标识符或'(''''''''''''''''''''''''''

1 个答案:

答案 0 :(得分:0)

%{这样的Yacc指令只能出现在yacc文件的FIRST部分(第一个%%之前),除了少数几个可以在第二个{第一个{之前} {1}})。第三部分(在第二个%%之后)只是将 verbatim 复制到输出y.tab.c文件中,因此除了C代码之外不能有任何其他内容。

看起来您只想在输出中使用该代码,因此只需删除第三部分中的%%%{行。然后,只需检查从编译器获得的每个错误或警告(并执行使用%})并找出他们说的内容以及如何解决它们。