parser doesn't fail, skipper doesn't skip - boost-spirit-x3

I have problem's with my grammar. I'm not sure why the date get parsed and why i doesn't need the lexeme parser.
Full Example
Live On Coliru
#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <string>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
namespace grammar
{
using namespace x3;
auto const term_ = rule<struct term_>{"term"}
= lexeme[ (lit("ppl_") | lit("type ") | lit("group ")) >> int_ ];
auto const entry_ = rule<struct entry_>{"entry"}
= +(term_ | float_);
auto const start_ = rule<struct start_>{"start"}
= (term_ >> entry_) % eol;
}
int main(int argc, const char * argv[])
{
std::string s{"ppl_1,group 2,type 1,2016-01-02"};
auto beg = std::begin(s);
auto end = std::end(s);
auto ret = x3::phrase_parse(beg, end, grammar::start_, x3::char_(','));
if(ret && beg == end)
std::cout << "all done\n";
else
std::cout << "Unparsed part's : '" << std::string(beg, std::next(beg, 10)) << "'\n";
return 0;
}
Output:
<start>
<try>ppl_1,group 2,type 1</try>
<term>
<try>ppl_1,group 2,type 1</try>
<success>,group 2,type 1,2016</success>
</term>
<entry>
<try>,group 2,type 1,2016</try>
<term>
<try>,group 2,type 1,2016</try>
<success>,type 1,2016-01-02</success>
</term>
<term>
<try>,type 1,2016-01-02</try>
<success>,2016-01-02</success>
</term>
<term>
<try>,2016-01-02</try>
<fail/>
</term>
<term>
<try>-01-02</try>
<fail/>
</term>
<term>
<try>-02</try>
<fail/>
</term>
<term>
<try></try>
<fail/>
</term>
<success></success>
</entry>
<success></success>
</start>
all done
Without the lexeme parser:
changed the rule term_ to:
auto const term_ = rule<struct term_>{"term"}
= (lit("ppl_") | lit("type ") | lit("group ")) >> int_ ;
I thought the skipper would try to skip after the lit() ?
<start>
<try>ppl_1,group 2,type 1</try>
<term>
<try>ppl_1,group 2,type 1</try>
<success>,group 2,type 1,2016</success>
</term>
<entry>
<try>,group 2,type 1,2016</try>
<term>
<try>,group 2,type 1,2016</try>
<success>,type 1,2016-01-02</success>
</term>
<term>
<try>,type 1,2016-01-02</try>
<success>,2016-01-02</success>
</term>
<term>
<try>,2016-01-02</try>
<fail/>
</term>
<term>
<try>-01-02</try>
<fail/>
</term>
<term>
<try>-02</try>
<fail/>
</term>
<term>
<try></try>
<fail/>
</term>
<success></success>
</entry>
<success></success>
</start>
all done

After the comment from jv_ who answered my question, here the working gramma:
namespace grammar
{
using namespace x3;
auto const date_ = rule<struct date_>{"time"}
= int_ >> '-' >> int_ >> '-' >> int_;
auto const term_ = rule<struct term_>{"term"}
= (lit("ppl_") | lit("type ") | lit("group ")) >> int_;
auto const entry_ = rule<struct entry_>{"entry"}
= (term_ | date_ | float_) % ',';
auto const start_ = rule<struct start_>{"start"}
= (term_ >> ',' >> entry_) % eol;
}
and the parser call without the skipper:
auto ret = x3::parse(beg, end, grammar::start_, peoples);

Related

Yacc parser not detecting my language well

I am new to yacc and I am trying to define some rules for my language.
I have written a grammar "well" and it runs and executes without an error but for some reason, it doesn't do what it is supposed to do.
mylex.l
%{
#include <stdio.h>
#include "myyacc.tab.h"
extern int yyval;
%}
/* KEEP TRACK OF LINE NUMBER*/
%option yylineno
uppercase [A-Z]
lowercase [a-z]
alpha [{uppercase}{lowercase}]
digit [0-9]
alphanum [{alpha}{digit}]
id uppercase({alphanum}|_)*
int_literal [0-9]+
float_literal [0-9]+\.[0-9]+
string_literal \"[^\"]*\"
comment (##)(.)*(##)
%%
"int" {return INT;}
"float" {return FLOAT;}
"boolean" {return BOOLEAN;}
"if" {return IF;}
"else" {return ELSE;}
"end" {return END;}
"true" {return TRUE;}
"false" {return FALSE;}
"read" {return READ;}
"print" {return PRINT;}
"while" {return WHILE;}
"START" {return START;}
"END" {return END;}
"+" {return ADD;}
"-" {return SUB;}
"*" {return MUL;}
"/" {return DIV;}
"&&" {return LOG_AND;}
"||" {return LOG_OR;}
"!" {return LOG_NOT;}
"==" {return EQ;}
"<>" {return NEQ;}
"<" {return LT;}
"<=" {return LEQ;}
">" {return GT;}
">=" {return GEQ;}
"=" {return ASSIGN;}
"(" {return LPAREN;}
")" {return RPAREN;}
"{" {return LBRACE;}
"}" {return RBRACE;}
{int_literal} {return INT_LITERAL;}
{float_literal} {return FLOAT_LITERAL;}
{string_literal} {return STRING_LITERAL;}
{id} {return ID;}
{comment} { ; }
%%
int yywrap() {
return 1;
}
myyacc.y
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylineno;
extern FILE* yyin;
extern int yyerror (char* msg);
extern char * yytext;
%}
/* definitions section start */
%token INT FLOAT BOOLEAN IF ELSE END TRUE FALSE READ PRINT WHILE START
%token INT_LITERAL FLOAT_LITERAL STRING_LITERAL ID ERROR
%right ASSIGN
%right LOG_NOT
%left MUL DIV
%left ADD SUB
%left LPAREN RPAREN
%left LBRACE RBRACE
%left LT LEQ GT GEQ
%left EQ NEQ
%left LOG_AND
%left LOG_OR
%start program
/* definitions section end */
%%
/* rules section start */
program : START statements END {printf("No syntax errors detected")};
statements : statements statement
| statement
;
statement : dec_stmt
| assignment_stmt
| print_stmt
| read_stmt
| condition_stmt
| while_stmt
;
dec_stmt : type ID
;
type : INT
| FLOAT
| BOOLEAN
;
assignment_stmt : ID ASSIGN expression
;
expression : exp EQ exp
| exp NEQ exp
| exp LT exp
| exp LEQ exp
| exp GT exp
| exp GEQ exp
| exp
;
exp : exp MUL exp
| exp DIV exp
| exp ADD exp
| exp SUB exp
| exp LOG_AND exp
| exp LOG_OR exp
| LOG_NOT exp
| LPAREN exp RPAREN
| INT_LITERAL
| FLOAT_LITERAL
| ID
| TRUE
| FALSE
;
print_stmt : PRINT LPAREN ID RPAREN
| PRINT LPAREN STRING_LITERAL RPAREN
;
read_stmt : ID ASSIGN READ LPAREN RPAREN
;
condition_stmt : IF LPAREN expression RPAREN LBRACE statement RBRACE END
| IF LPAREN expression RPAREN LBRACE statement RBRACE ELSE LBRACE statement RBRACE END
;
while_stmt : WHILE LPAREN expression RPAREN LBRACE statement RBRACE
;
/* rules section end */
%%
/* auxiliary routines start */
int main(int argc, char *argv[])
{
// don't change this part
yyin = fopen(argv[1], "r" );
if(!yyparse())
printf("\nParsing complete\n");
else
printf("\nParsing failed\n");
fclose(yyin);
return 0;
}
int yyerror (char* msg)
{
printf("Line %d: %s near %s\n", yylineno, msg, yytext);
exit(1);
}
/* auxiliary routines end */
Test case
START
int X12
float ABC1
DDe = 7
while(QNn >0) ## this a Comment ##
{ RLk9999 = ACc - 2
CCC = true
}
if ( ACc ==5){ print ( " Inside IF inside Loop " ) } end }
print ( " Hello .. " )
END
Output
Line 3: syntax error near 12
It also gets the line number wrong.
I've been trying to see what I'm doing wrong for some time now and I'd really appreciate a second set of eyes.
You cannot use macros inside character classes. Inside a character class, pattern operators lose their special meaning, so when you write
alphanum [{alpha}{digit}]
you are defining a character class containing {, }, and the letters adghilpt. That doesn't match the 12 in X12.
Anyway, flex already has predefined sets of characters which you can include in your character classes:
* [:lower:] a-z
* [:upper:] A-Z
* [:alpha:] [:lower:][:upper:]
* [:digit:] 0-9
* [:alnum:] [:alpha:][:digit:]
Note that these can only be used inside a character class. So you could write your id pattern as
id [[:upper:]][[:alnum:]_]*
without the need for any other macros.
Please see the flex pattern documentation for more details.
In addition to #rici's answer, I've also noticed that my while_statement in the yacc file has only been set to accept only one statement in it's body

Lex Yacc parser compiling get few errors tried searching solution cant find

We have a task to compile a lex and a yacc praser code then run them together by using the cc tab.y.c -ll -Ly command when we do each apart they compile just fine but the compile both parts as one gives 10 lines of errors.
First part is 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 code:
%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;
}
the errors we get are the following:
[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
will appriciate the help of anyone who can tell us whats wrong here we have tried many things we still get these errors ..
If you look at line 63 of test.y, as indicated in the first error message, you will see the first problem; you misspelled typedef. Fix that, and then check remaining errors, if any, by looking at the indicated lines.

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

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
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.

Bison:syntax error at the end of parsing

Hello this is my bison grammar file for a mini-programming language:
%{
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "projectbison.tab.h"
void yyerror(char const *);
extern FILE *yyin;
extern FILE *yyout;
extern int yylval;
extern int yyparse(void);
extern int n;
int errNum = 0;
int forNum = 0;
%}
%left PLUS MINUS
%left MULT DIV MOD
%nonassoc EQUAL NEQUAL LESS GREATER LEQUAL GEQUAL
%token INTEGER BOOLEAN STRING VOID
%token ID
%token AND
%token BEGINP
%token ENDP
%token EXTERN
%token COMMA
%token EQ
%token RETURN1
%token IF1 ELSE1 WHILE1 FOR1 DO1
%token LOR LAND LNOT
%token TRUE FALSE
%token EQUAL NEQUAL LESS GREATER LEQUAL GEQUAL
%token LB1 RB1
%token LCB1 RCB1
%token SEMIC
%token NEWLINE
%token PLUS MINUS
%token MULT DIV MOD
%token DIGIT STRING1
%start program
%%
/*50*/
program : external-decl program-header defin-field command-field
;
external-decl : external-decl external-prototype
|
;
external-prototype : EXTERN prototype-func NEWLINE
;
program-header : VOID ID LB1 RB1 NEWLINE
;
defin-field : defin-field definition
|
;
definition : variable-defin
| func-defin
| prototype-func
;
variable-defin : data-type var-list SEMIC newline
;
data-type : INTEGER
| BOOLEAN
| STRING
;
var-list : ID extra-ids
;
extra-ids : COMMA var-list
|
;
func-defin : func-header defin-field command-field
;
prototype-func : func-header SEMIC
;
func-header : data-type ID LB1 lists RB1 newline
;
lists: list-typ-param
|
;
list-typ-param : typical-param typical-params
;
typical-params : COMMA list-typ-param
|
;
typical-param : data-type AND ID
;
command-field : BEGINP commands newline ENDP newline
;
commands : commands newline command
|
;
command : simple-command SEMIC
| struct-command
| complex-command
;
complex-command : LCB1 newline command newline RCB1
;
struct-command : if-command
| while-command
| for-command
;
simple-command : assign
| func-call
| return-command
| null-command
;
if-command : IF1 LB1 gen-expr RB1 newline command else-clause
;
else-clause: ELSE1 newline command
;
while-command : WHILE1 LB1 gen-expr RB1 DO1 newline RCB1 command LCB1
;
for-command : FOR1 LB1 conditions RB1 newline RCB1 command LCB1
;
conditions : condition SEMIC condition SEMIC condition SEMIC
;
condition : gen-expr
|
;
assign : ID EQ gen-expr
;
func-call : ID LB1 real-params-list RB1
| ID LB1 RB1
;
real-params-list : real-param real-params
;
real-params : COMMA real-param real-params
|
;
real-param : gen-expr
;
return-command : RETURN1 gen-expr
;
null-command :
;
gen-expr : gen-terms gen-term
;
gen-terms : gen-expr LOR
|
;
gen-term : gen-factors gen-factor
;
gen-factors : gen-term LAND
|
;
gen-factor : LNOT first-gen-factor
| first-gen-factor
;
first-gen-factor : simple-expr comparison
| simple-expr
;
comparison : compare-operator simple-expr
;
compare-operator : EQUAL
| NEQUAL
| LESS
| GREATER
| LEQUAL
| GEQUAL
;
simple-expr : expresion simple-term
;
expresion : simple-expr PLUS
|simple-expr MINUS
|
;
simple-term : mul-expr simple-parag
;
mul-expr: simple-term MULT
| simple-term DIV
| simple-term MOD
|
;
simple-parag : simple-prot-oros
| MINUS simple-prot-oros
;
simple-prot-oros : ID
| constant
| func-call
| LB1 gen-expr RB1
;
constant : DIGIT
| STRING1
| TRUE
| FALSE
;
newline:NEWLINE
|
;
%%
void yyerror(char const *msg)
{
errNum++;
fprintf(stderr, "%s\n", msg);
}
int main(int argc, char **argv)
{
++argv;
--argc;
if ( argc > 0 )
{yyin= fopen( argv[0], "r" ); }
else
{yyin = stdin;
yyout = fopen ( "output", "w" );}
int a = yyparse();
if(a==0)
{printf("Done parsing\n");}
else
{printf("Yparxei lathos sti grammi: %d\n", n);}
printf("Estimated number of errors: %d\n", errNum);
return 0;
}
for a simple input like this :
void main()
integer k;
boolean l;
begin
aek=32;
end
i get the following :
$ ./MyParser.exe file2.txt
void , id ,left bracket , right bracket
integer , id ,semicolon
boolean , id ,semicolon
BEGIN PROGRAM
id ,equals , digit ,semicolon
END PROGRAM
syntax error
Yparxei lathos sti grammi: 8
Estimated number of errors: 1
And whatever change i make to the input file i get a syntax error at the end....Why do i get this and what can i do??thanks a lot in advance!here is the flex file just in case someone needs it :
%{
#include "projectbison.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n=1;
%}
%option noyywrap
digit [0-9]+
id [a-zA-Z][a-zA-Z0-9]*
%%
"(" {printf("left bracket , "); return LB1;}
")" {printf("right bracket\n"); return RB1;}
"{" {printf("left curly bracket , "); return LCB1;}
"}" {printf("right curly bracket\n"); return RCB1;}
"==" {printf("isotita ,"); return EQUAL;}
"!=" {printf("diafora ,"); return NEQUAL;}
"<" {printf("less_than ,"); return LESS;}
">" {printf("greater_than ,"); return GREATER;}
"<=" {printf("less_eq ,"); return LEQUAL;}
">=" {printf("greater_eq ,"); return GEQUAL;}
"||" {printf("lor\n"); return LOR;}
"&&" {printf("land\n"); return LAND;}
"&" {printf("and ,"); return AND;}
"!" {printf("lnot ,"); return LNOT;}
"+" {printf("plus ,"); return PLUS; }
"-" {printf("minus ,"); return MINUS;}
"*" {printf("multiply ,"); return MULT;}
"/" {printf("division ,"); return DIV;}
"%" {printf("mod ,"); return MOD;}
";" {printf("semicolon \n"); return SEMIC;}
"=" {printf("equals , "); return EQ;}
"," {printf("comma ,"); return COMMA;}
"\n" {n++; return NEWLINE;}
void {printf("void ,"); return VOID;}
return {printf("return ,"); return RETURN1;}
extern {printf("extern\n"); return EXTERN;}
integer {printf("integer ,"); return INTEGER;}
boolean {printf("boolean ,"); return BOOLEAN;}
string {printf("string ,"); return STRING;}
begin {printf("BEGIN PROGRAM\n"); return BEGINP;}
end {printf("END PROGRAM\n"); return ENDP;}
for {printf("for\n"); return FOR1;}
true {printf("true ,"); return TRUE;}
false {printf("false ,"); return FALSE;}
if {printf("if\n"); return IF1; }
else {printf("else\n"); return ELSE1; }
while {printf("while\n"); return WHILE1;}
{id} {printf("id ,"); return ID;}
{digit} {printf("digit ,"); return DIGIT;}
[a-zA-Z0-9]+ {return STRING1;}
` {/*catchcall*/ printf("Mystery character %s\n", yytext); }
<<EOF>> { static int once = 0; return once++ ? 0 : '\n'; }
%%
Your scanner pretty well guarantees that two newline characters will be sent at the end of the input: one from the newline present in the input, and another one as a result of your trapping <<EOF>>. However, your grammar doesn't appear to accept unexpected newlines, so the second newline will trigger a syntax error.
The simplest solution would be to remove the <<EOF>> rule, since text files without a terminating newline are very rare, and it is entirely legitimate to consider them syntax errors. A more general solution would be to allow any number of newline characters to appear where a newline is expected, by defining something like:
newlines: '\n' | newlines '\n';
(Using actual characters for single-character tokens makes your grammar much more readable, and simplifies your scanner. But that's a side issue.)
You might also ask yourself whether you really need to enforce newline terminators, since your grammar seems to use ; as a statement terminator, making the newline redundant (aside from stylistic considerations). Removing newlines from the grammar (and ignoring them, as with other whitespace, in the scanner) will also simplify your code.

bison grammar rules for a custom pascal like language

I'm trying to make a compiler for a custom pascal like language using bison and flex and I end up getting syntax errors for programs that should be correct according to my custom grammar.
My custom grammar:
<program> ::= program id
<block>
<block> ::= {
<sequence>
}
<sequence> ::= <statement> ( ; <statement> )*
<brackets-seq> ::= { <sequence> }
<brack-or-stat> ::= <brackets-seq> |
<statement>
<statement> ::= ε |
<assignment-stat> |
<if-stat> |
<while-stat>
<assignment-stat> ::= id := <expression>
<if-stat> ::= if (<condition>)
<brack-or-stat>
<elsepart>
<elsepart> ::= ε |
else <brack-or-stat>
<while-stat> ::= while (<condition>)
<brack-or-stat>
<expression> ::= <optional-sign> <term> ( <add-oper> <term>)*
<term> ::= <factor> (<mul-oper> <factor>)*
<factor> ::= constant |
(<expression>) |
id
<condition> ::= <boolterm> (and <boolterm>)*
<boolterm> ::= <boolfactor> (or <boolfactor>)*
<boolfactor> ::= not [<condition>] |
[<condition>] |
<expression> <relational-oper> <expression>
<relational-oper> ::= == | < | > | <> | <= | >=
<add-oper> ::= + | -
<mul-oper> ::= * | /
<optional-sign> ::= ε | <add-oper>
My grammar implementation on bison:
%{
#include <stdio.h>
#include <string.h>
int yylex(void);
void yyerror(char *s);
%}
%union {
int i;
char *s;
};
%token <i> INTEGERNUM
%token PROGRAM;
%token OR;
%token AND;
%token NOT;
%token IF;
%token ELSE;
%token WHILE;
%token PLUS;
%token MINUS;
%token MUL;
%token DIV;
%token LSB;
%token RSB;
%token LCB;
%token RCB;
%token LEFTPAR;
%token RIGHTPAR;
%token ID;
%token INT;
%token ASSIGN;
%token ISEQUAL;
%token LTHAN;
%token GTHAN;
%token NOTEQUAL;
%token LESSEQUAL;
%token GREATEREQUAL;
%left '+' '-'
%left '*' '/'
%%
program:
PROGRAM ID block
;
block:
LCB RCB
|LCB sequence RCB
;
sequence:
statement ';'sequence
|statement ';'
;
bracketsSeq:
LCB sequence RCB
;
brackOrStat:
bracketsSeq
|statement
;
statement:
assignmentStat
|ifStat
|whileStat
|
;
assignmentStat:
ID ':=' expression
ifStat:
IF LEFTPAR condition RIGHTPAR brackOrStat elsepart
;
elsepart:
ELSE brackOrStat
|
;
whileStat:
WHILE LEFTPAR condition RIGHTPAR brackOrStat
;
expression:
addOper expression
|expression addOper expression
|term
;
term:
term mulOper term
|factor
;
factor:
INT
|LEFTPAR expression RIGHTPAR
|ID
;
condition:
condition AND condition
|boolterm
;
boolterm:
boolterm OR boolterm
|boolfactor
;
boolfactor:
NOT LSB condition RSB
|LSB condition RSB
|expression relationalOper expression
;
relationalOper:
ISEQUAL
|LTHAN
|GTHAN
|NOTEQUAL
|LESSEQUAL
|GREATEREQUAL
;
addOper:
PLUS
|MINUS
;
mulOper:
MUL
|DIV
;
optionalSign
|addOper
;
%%
int main( int argc, char **argv )
{
extern FILE *yyin;
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
do
yyparse();
while(!feof(yyin));
}
My flex implementation is pretty straightforward where I just return tokens for each symbol or identifier needed.
Using my implementation on the following simple program:
program circuit
{
a:=b;
}
I end up getting a syntax error. Specifically when the parsing reaches the point right after := according to my debugging prints I use:
$ ./a.exe verilog.txt
text = program
text = circuit val = circuit
text = {
text = a val = a
text = :=
syntax error
This is the first time I use flex and bison so I'm guessing that I made a wrong implementation of my original grammar to bison since after the ./bison.exe -dy comp.y command I get:
bison conflicts 64 shift/reduce
Any ideas would be helpful. Thanks!
This rule :
assignmentStat: ID ':=' expression
uses a token ':=' which bison gives a code distinct from any other token, and which your lexer has no way of knowing, so you're almost certainly not returning it. You're probably returning ASSIGN for the character sequence ':=', so you want:
assignmentStat: ID ASSIGN expression
For the shift-reduce conflicts, they mean that the parser doesn't match exactly the language you specified, but rather some subset (as determined by the default shift instead of reduce). You can use bison's -v option to get a complete printout of the parser state machine (including all the conflicts) in a .output file. You can then examine the conflicts and determine how you should change the grammar to match what you want.
When I run bison on your example, I see only 9 shift/reduce conflicts, all arising from expr: expr OP expr-style rules, which are ambiguous (may be either right- or left- recursive). The default resolution (shift) makes them all right-recursive, which may not be what you want. You can either change the grammar to not be ambiguous, or use bison's built-in precedence resolution tools to resolve them.