Error: "expected identifier or '(' before '%' token" (Csimple parser in yacc)

1.3k Views Asked by At

im trying to build a compiler for Csimple. when i run the command "cc –o test y.tab.c –ll –L.y" i get a wall of errors. i am at a loss. im aware that my yacc file doesnt parse well, but first i need it to run so i can see the output. the error appears for the line "%{" in the middle of the yacc file.

lex:

%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;
}

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>
    #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;
}

executes:

lex test.l
yacc test.y
cc –o test y.tab.c –ll –L.y

this is where it all breaks down.

as i said, i get ALOT of errors, but the first one is test.y:60: error: expected identifier or ‘(’ before ‘%’ token

1

There are 1 best solutions below

0
On

Yacc directives like %{ can only appear in the FIRST section of the yacc file (before the first %%), with the exception of a few that can be in the second (before the second %%). The third section (after the second %%) is just copied verbatim to the output y.tab.c file, so there can't be anything except C code in it.

It looks like you just want that code in the output, so just delete those %{ and %} lines in the third section. Then, just go through each error or warning you get from the compiler (and do use -Wall) and figure out what they say and how to fix them.