Skip to content

YACC Examples

YACC Program

in2post.y

%{
    #include <stdio.h>
%}
%token DIGIT PLUS STAR
%%
start : expr '\\n'           {printf("\\nComplete");exit(1);}
    ;
expr:  expr PLUS expr        {printf("+ ");}
    | expr STAR expr     {printf("\* ");}
    | '(' expr ')'
    | DIGIT             {printf("%d ",$1);}
    ;
%%


yyerror()
{
    printf("Error");
}
main()
{
    yyparse();
    return 1;
}

LEX Program

in2post.l

%{
    #include <stdio.h>
    #include "y.tab.h"
%}
%%
[0-9]+ return DIGIT;
'+' return PLUS;
'*' return STAR;
'(' return \*yytext;
')' return \*yytext;
%%
yywrap()
{
    return 1;
}
Back to top