1

I am trying to make a syntax analyzer that will recognize a valid statement and will print success upon doing so. However, after making the Lex and Yacc files, I keep getting errors in my Yacc file which says:

In function 'yyparse'fofo.y: In function 'yyparse':
fofo.y:13:5: error: stray '\223' in program
fofo.y:13:5: error: stray '' in program
fofo.y:13:16: error: 'n' undeclared (first use in this function)
fofo.y:13:16: note: each undeclared identifier is reported only once for each function it appears in
fofo.y:13:18: error: expected ')' before 'Invalid'
fofo.y:13:18: error: stray '' in program
fofo.y:13:18: error: stray '\224' in program

Here's my Yacc file contents:

%{
#include <stdio.h>
%}

%start Stmt_list
%token Id Num Relop Addop Mulop Assignop Not

%%
Stmt_list    : Stmt ';' '\n'    {printf ("\n Success. \n"); exit(0);}
        | Stmt_list Stmt ';' '\n'    {printf ("\n Success. \n"); exit(0);}
        | error '\n'    {printf (“\n Invalid. \n”); exit(1);}
        ;

Stmt    : Variable Assignop Expression
    ;

Variable    : Id
        | Id '['Expression']'
        ;

Expression    : Simple_expression
        | Simple_expression Relop Simple_expression
        ;

Simple_expression    : Term
            | Simple_expression Addop Term
            ;

Term    : Factor
    | Term Mulop Factor
    ;

Factor    : Id
    | Num
    | '('Expression')'
    | Id '['Expression']'
    | Not Factor
    ;

%%

#include"lex.yy.c"

int main()
{
    yyparse();
    yylex();

}

yyerror(char *s)
{
    printf("\nError\n");
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Zubair
  • 25
  • 3
  • 9
  • 223 (octal. 0x93 hexadecimal) is likely the stand-alone [CE/CP-1250](https://en.wikipedia.org/wiki/Windows-1250), corresponding to U+201C ([LEFT SINGLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8212&number=128)). 224 is likely the stand-alone CE/CP-1250, corresponding to U+201D ([RIGHT SINGLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8212&number=128)). – Peter Mortensen May 27 '23 at 00:09
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 27 '23 at 00:13

1 Answers1

1

The errors come from having some non ASCII characters in the text (which probably come from pasting text from a Word file), on line 13, as the error message indicated:

        | error '\n'    {printf (“\n Invalid. \n”); exit(1);}
                                 ^              ^
                                 |              |
                                 `--------------`------------   The error is here!

Note the quotation characters are different to the line above, and should be edited to be:

        | error '\n'    {printf ("\n Invalid. \n"); exit(1);}

I also added some white space around your tokens. For example, on these lines:

        | Id '['Expression']'
    | '('Expression')' 
    | Id '['Expression']'

which I changed to:

        | Id '[' Expression ']'
    | '(' Expression ')' 
    | Id '[' Expression ']'

I also note you are calling the C function 'exit' but have not declared it properly. You need the following line in your header:

#include <stdlib.h>

It then seemed to build OK for me.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • the bracket tokens around the expressions you meant? I see. i'll have a go. – Ali Zubair Nov 08 '15 at 13:04
  • fofo.y:43:6: warning: conflicting types for 'yyerror' [enabled by default] y.tab.c:1398:7: note: previous implicit declaration of 'yyerror' was here yyerror (YY_("syntax error")); ^ – Ali Zubair Nov 08 '15 at 13:08