0

I made my ex1.l file and ex1.y file to make lexical analyzer and parser. I comiled them with command "lex ex1.l" and "yacc -d -v ex1.y". So y.output file has been made. This is a source code of them.

  1. ex1.l
%{
#include <stdio.h>
#include "y.tab.h"
%}

D   [0-9]
L   [a-zA-Z_]

%%
"int"   {return INT;}
{L}({L}|{D})*   {return IDENTIFIER;}
{D}+    {return NUMBER;}
"=" {return '=';}
";" {return ';';}
"," {return ',';}
[\t\v\n\f .]    {;}
%%

int yywrap(void){
    return 1;
}
2. ex1.y
%{
#include <stdio.h>
int yylex();
int intcount=0;
%}
%token NUMBER INT IDENTIFIER
%start start_state

%%
start_state: INT declaration_list ';'
    ;
declaration_list: declaration_list ',' declaration
    | declaration
    ;
declaration: IDENTIFIER '=' NUMBER {intcount++;}
    | IDENTIFIER{intcount++;}
    ;
%%
int main(void){
    printf("first int count is %d \n\n",intcount);
    yyparse();
    printf("now int count is %d \n\n",intcount);
    return 0;
}
void yyerror(const char * str){
    fprintf(stderr,"error : %s\n",str);
}

And this is my y.output file

0  $accept : start_state $end

   1  start_state : INT declaration_list ';'

   2  declaration_list : declaration_list ',' declaration
   3                   | declaration

   4  declaration : IDENTIFIER '=' NUMBER
   5              | IDENTIFIER


state 0
    $accept : . start_state $end  (0)

    INT  shift 1
    .  error

    start_state  goto 2


state 1
    start_state : INT . declaration_list ';'  (1)

    IDENTIFIER  shift 3
    .  error

    declaration_list  goto 4
    declaration  goto 5


state 2
    $accept : start_state . $end  (0)

    $end  accept


state 3
    declaration : IDENTIFIER . '=' NUMBER  (4)
    declaration : IDENTIFIER .  (5)

    '='  shift 6
    ';'  reduce 5
    ','  reduce 5


state 4
    start_state : INT declaration_list . ';'  (1)
    declaration_list : declaration_list . ',' declaration  (2)

    ';'  shift 7
    ','  shift 8
    .  error


state 5
    declaration_list : declaration .  (3)

    .  reduce 3


state 6
    declaration : IDENTIFIER '=' . NUMBER  (4)

    NUMBER  shift 9
    .  error


state 7
    start_state : INT declaration_list ';' .  (1)

    .  reduce 1


state 8
    declaration_list : declaration_list ',' . declaration  (2)

    IDENTIFIER  shift 3
    .  error

    declaration  goto 10


state 9
    declaration : IDENTIFIER '=' NUMBER .  (4)

    .  reduce 4


state 10
    declaration_list : declaration_list ',' declaration .  (2)

    .  reduce 2


8 terminals, 4 nonterminals
6 grammar rules, 11 states

I want to know meanings of y.output file. First of all, I understood that there is 6 grammar rules. But I want to know the meaning of state. For example in case of state 3, Is there two kinds of input declaration : IDENTIFIER . '=' NUMBER (4) and declaration : IDENTIFIER . (5)? And then when next token is '=', parser put it in the stack(shift) and go to state 6? Likewise when next token is ';' or ',', do I have to pop off the stack(reduce) and go to state 5? Is there anybody who can help me to understand about the meaning of y.output file??

SeungWan
  • 13
  • 2
  • You've described it pretty well but you do not have to do anything with that. It just says what the parser does internally. It is useful for debugging a broken grammar or to understand how the parser works but generally you don't need this file to write a yacc program. – Piotr Siupa May 07 '23 at 00:30
  • Umm....Then what is a reasonable way of debugging when my yacc program doesn't work well? – SeungWan May 07 '23 at 06:13
  • Turn on debugging (https://stackoverflow.com/q/50821203/3052438) and use the `y.output` as a reference. – Piotr Siupa May 07 '23 at 08:08

0 Answers0