0
votes

I have a lex program listed below, it recognizes (lexical analysis)specific tokens and outputs them to the screen when input from the keyboard. I want to use yacc for syntactical analysis of the code below, but i don't quite get this part.....what would the output look like? Is my code below sufficient? I've read many articles online about yacc....just need someone to give a simple explanation. Thanks in advance...

/* Regular Definitions */
RelationalOp_1         "<"
RelationalOp_2      "<="
RelationalOp_3      "=<"    
RelationalOp_4      "=="
RelationalOp_5      "!="
RelationalOp_6      ">"
RelationalOp_7      ">="
RelationalOp_8      "=>"

%%

{RelationalOp_1}        {printf("(RelationalOp_1, %s)\n", yytext);}
{RelationalOp_2}        {printf("(RelationalOp_2, %s)\n", yytext);}
{RelationalOp_3}        {printf("(RelationalOp_3, %s)\n", yytext);}
{RelationalOp_4}        {printf("(RelationalOp_4, %s)\n", yytext);}
{RelationalOp_5}        {printf("(RelationalOp_5, %s)\n", yytext);}
{RelationalOp_6}        {printf("(RelationalOp_6, %s)\n", yytext);}
{RelationalOp_7}        {printf("(RelationalOp_7, %s)\n", yytext);}
{RelationalOp_8}        {printf("(RelationalOp_8, %s)\n", yytext);}

.                            printf("Invalid input: %s\n",yytext);

%%

main()
{
  yylex();
}

yywrap()
{
}
1
This is just writing operation name and token ID while parsing the file, what you want to do?Roozbeh Zabihollahi
After having learnt flex and yacc/bison, I just decided to roll my own parser.user529758
Well when you start doing that and have a real question let us know.user207421

1 Answers

0
votes

The short answer to your question is no, what you have is incomplete for working with a yacc-generated parser. In your lex-generated scanner, you want to provide tokens in memory to the parser, not simply write something to the console.

For understanding context, Pete Jinks's lecture notes are comprehensive, and show how lex- and yacc-generated programs work together. You might also look at the online manual for Bison , which is the GNU version of yacc.