warning: nonterminal useless in grammar: const_declaration [-Wother] - parsing

I have the following yacc grammar:
%{
#include <stdio.h>
extern FILE* yyin;
extern char* yytext;
%}
%token VAR ID_NAME TYPE_STRING TYPE_BOOL TYPE_NUMBER CONST
%%
var_declaration: VAR ':' type ID_NAME ';' { printf("var\n"); }
;
const_declaration: CONST ':' type ID_NAME ';' {printf("const\n");}
;
type: TYPE_NUMBER
| TYPE_STRING
| TYPE_BOOL
;
%%
void yyerror (char const *s) {
fprintf (stderr, "%s\n", s);
}
int main(int argc, char** argv[])
{
yyparse();
return 0;
}
It should describe a little language that at this time should allow variable declarations of the form var:<type> <name>; and constants declarations of the form const:<type> <name>;.
When I run yacc -vd grammar.y I get:
yacc -vd grammar.y
grammar.y: warning: 1 nonterminal useless in grammar [-Wother]
grammar.y: warning: 1 rule useless in grammar [-Wother]
grammar.y:16.1-17: warning: nonterminal useless in grammar: const_declaration [-Wother]
const_declaration: CONST ':' type ID_NAME ';' {printf("const\n");}
^^^^^^^^^^^^^^^^^
grammar.y:16.20-67: warning: rule useless in grammar [-Wother]
const_declaration: CONST ':' type ID_NAME ';' {printf("const\n");}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Why does it say that const_declaration rule is useless?

The reason you get this error message is that the rule const_declaration does not appear in any other rule, and is thus not needed for the grammar.
The rule var_declaration is taken as the start rule (as you have not specified a start it uses the first one). This rule only uses the type rule and thus all other rules are redundant, which is what it is telling you.
Perhaps this is a subset of a larger grammar? When all the grammar is in the file and the const_declaration rule is used somewhere the error will go away.

Related

YACC: How to pass the string value of a token to the yacc program

I want to design a program using lex and yacc that will take input in Morse code form and the parser would generate their English meaning.
This is the lex file:
%{
#include <stdio.h>
int i=0;
char c;
%}
NewLine ".-.-"
EOF ".-.-"
NewPara "-...-"
Over "K"
SOS "...---..."
%%
".." { yylval=(char *)calloc(yyleng,sizeof(char));
strcpy(yylval,yytext);
return (I);}
".-""--" { yylval=(char *)calloc(yyleng,sizeof(char));
strcpy(yylval,yytext);
return (AM);}
".-""...-"".-""-."".." {yylval.value=(char *)calloc(yyleng,sizeof(char));
strcpy(yylval,yytext);
return (AVANI);}
. return yytext[0];
%%
The yacc file:
%{
#include <stdio.h>
#define YYSTYPE char*
char message [20];
%}
%token I AM AVANI
%start msg
%%
msg : Np Vp {printf("The sentence is I AM AVANI");}
;
Np : I
;
Vp : Aux N
;
Aux : AM
;
N : AVANI
;
%%
#include "lex.yy.c"
/*extern YYSTYPE yylval*/
void main(){
printf("Enter the message\n");
scanf("%s",message);
yyparse();
}
void yyerror(const char *s)
{
fprintf(stderr,"s\n",s);
}
yywrap ()
{
return 1;
}
This shows the following error on Compiling:
morse_code.l: In function ‘yylex’:
morse_code.l:13:10: warning: assignment makes integer from pointer
without a cast [enabled by default]
".." { yylval=(char *)calloc(yyleng,sizeof(char));
^
morse_code.l:14:2: warning: passing argument 1 of ‘strcpy’ makes
pointer from integer without a cast [enabled by default]
strcpy(yylval,yytext);
^
In file included from lex.yy.c:20:0:
/usr/include/string.h:125:14: note: expected ‘char * __restrict__’ but
argument is of type ‘YYSTYPE’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
morse_code.l:17:9: warning: assignment makes integer from pointer
without a cast [enabled by default]
".-""--" { yylval=(char *)calloc(yyleng,sizeof(char));
^
morse_code.l:18:5: warning: passing argument 1 of ‘strcpy’ makes
pointer from integer without a cast [enabled by default]
strcpy(yylval,yytext);
^
In file included from lex.yy.c:20:0:
/usr/include/string.h:125:14: note: expected ‘char * __restrict__’ but
argument is of type ‘YYSTYPE’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
morse_code.l:21:8: error: request for member ‘value’ in something not
a structure or union
".-""...-"".-""-."".." {yylval=(char *)calloc(yyleng,sizeof(char));
^
morse_code.l:22:4: warning: passing argument 1 of ‘strcpy’ makes
pointer from integer without a cast [enabled by default]
strcpy(yylval,yytext);
^
In file included from lex.yy.c:20:0:
/usr/include/string.h:125:14: note: expected ‘char * __restrict__’ but
argument is of type ‘YYSTYPE’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
It'll be great if someone could help me with the correct way to pass the string value of a token to the yacc file.
Two obvious problems
You're missing a declaration for yylval in your .l file. If you include the "y.tab.h" generated by yacc, that will define it for you, but you don't have that either.
you don't allocate space for the terminating NUL character of your strings with calloc, so when you strcpy the string it will write one byte past the end of your allocation, causing undefined behavior.

Error while retrieving value from symbol table

I'm trying to implement a simple calculator using Flex and Bison. I'm running into problems in the Bison stage, wherein I can't figure out the way in which the value of the variable can be retrieved from the symbol table and assigned to $$.
The lex file:
%{
#include <iostream>
#include <string.h>
#include "calc.tab.h"
using namespace std;
void Print();
int count = 0;
%}
%%
[ \t\n]+ ;
"print" {Print();}
"exit" {
exit(EXIT_SUCCESS);
}
[0-9]+ {
yylval.FLOAT = atof(yytext);
return (NUMBER);
count++;
}
[a-z][_a-zA-Z0-9]* {
yylval.NAME = yytext;
return (ID);
}
. {
return (int)yytext[0];
}
%%
void Print()
{
cout << "Printing ST..\n";
}
int yywrap()
{
return 0;
}
The Bison file:
%{
#include <iostream>
#include <string.h>
#include "table.h"
extern int count;
int yylex();
int yyerror(const char *);
int UpdateSymTable(float, char *, float);
using namespace std;
%}
%union
{
float FLOAT;
char *NAME;
}
%token NUMBER
%token ID
%type <FLOAT> NUMBER
%type <NAME> ID
%type <FLOAT> expr
%type <FLOAT> E
%left '*'
%left '/'
%left '+'
%left '-'
%right '='
%%
E: expr {cout << $$ << "\n";}
expr: NUMBER {$$ = $1;}
| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| ID '=' expr {
int index = UpdateSymTable($$, $1, $3);
$$ = st[index].number = $3; //The problem is here
}
%%
int yyerror(const char *msg)
{
cout << "Error: "<<msg<<"\n";
}
int UpdateSymTable(float doll_doll, char *doll_one, float doll_three)
{
int number1 = -1;
for(int i=0;i<count;i++)
{
if(!strcmp(doll_one, st[i].name) == 0)
{
strcpy(st[i].name, doll_one);
st[i].number = doll_three;
number1 = i;
}
else if(strcmp(doll_one, st[i].name) == 0)
{
number1 = i;
}
}
return number1;
}
int main()
{
yyparse();
}
The symbol table:
struct st
{
float number;
char name[25];
}st[25];
The output I'm getting is:
a = 20
c = a+3
20
Error: syntax error
I would really appreciate it if someone told me what is going wrong. I'm trying since a long time, and I haven't been able to resolve the error.
The syntax error is the result of your grammar only accepting a single expr rather than a sequence of exprs. See, for example, this question.
One of the problems with your symbol table lookup is that you incorrectly return the value yytext as your semantic value, instead of making a copy. See, for example, this question.
However, your UpdateSymTable functions has quite a few problems, starting with the fact that the names you chose for parameters are meaningless, and furthermore the first parameter ("doll_doll") is never used. I don't know what you intended to test with !strcmp(doll_one, st[i].name) == 0 but whatever it was, there must be a simpler way of expressing it. In any case, the logic is incorrect. I'd suggest writing some simple test programs (without bison and flex) to let you debug the symbol table handling. And/or talk to your lab advisor, assuming you have one.
Finally, (of what I noticed) your precedence relations are not correct. First, they are reversed: the operator which binds least tightly (assignment) should come first. Second, it is not the case that + has precedence over - , or vice versa; the two operators have the same precedence. Similarly with * and /. You could try reading the precedence chapter of the bison manual if you don't have lecture notes or other information.

Yacc parser for nested while loop

i was trying to code a parser using yacc and lex that count the number of nested loops (while or for).I started the implementation for just while loops.But for some reason the parser gives me an error at the end of a closing brace.
Here is the code.
%{
#include<stdio.h>
/*parser for counting while loops*/
extern int yyerror(char* error);
int while_count=0;
extern int yylex();
%}
%token NUMBER
%token VAR
%token WHILE
%%
statement_list : statement'\n'
| statement_list statement'\n'
;
statement :
while_stmt '\n''{' statement_list '}'
| VAR '=' NUMBER ';'
;
while_stmt :
WHILE '('condition')' {while_count++;}
;
condition :
VAR cond_op VAR
;
cond_op : '>'
| '<'
| '=''='
| '!''='
;
%%
int main(void){
yyparse();
printf("while count:%d\n",while_count);
}
int yyerror(char *s){
printf("Error:%s\n",s);
return 1;
}
what is wrong with that code.And is there a way in yacc to mention optional arguments? like the "\n" after while?
here is the lexer code
%{
#include"y.tab.h"
/*lexer for scanning nested while loops*/
%}
%%
[\t ] ; /*ignore white spaces*/
"while" {return WHILE;}
[a-zA-Z]+ {return VAR;}
[0-9]+ {return NUMBER;}
'$' {return 0;}
'\n' {return '\n' ;}
. {return yytext[0];}
%%
VAR is a variable name with just ascii characters and WHILE is the keyword while.type is not taken into consideration on variable assignments
The problem you seem to be having is with empty loop bodies, not nested loops. As written, your grammar requires at least one statement in the while loop body. You can fix this by allowing empty statement lists:
statement_list: /* empty */
| statement_list statement '\n'
;
You also ask about making newlines optional. The easiest way is to make the lexer simply discard newlines (as whitespace) rather than returning them. Then just get rid of the newlines in the grammar, and newlines can appear between any two tokens and will be ignored.
If you really must have newlines in the grammar for some reason, you can add a rule like:
opt_newlines: /* empty */ | opt_newlines '\n' ;
and then use this rule wherever you want to allow for newlines (replace all the literal '\n' in your grammar.) You have to be careful not to use it redundantly, however. If you do something like:
statement_list: /* empty */
| statement_list statement opt_newlines
;
while_stmt opt_newlines '{' opt_newlines statement_list opt_newlines '}'
you'll get shift/reduce conflicts as newlines before the } in a loop could be either part of the opt_newlines in the while or the opt_newlines in the statement_list. Its pretty easy to deal such conflicts by just removing the redundant opt_newlines.

parser for expressions in lex/yacc

I'm trying to parse statements to see whether they are valid or not according to these rules:
assignment:
id = exp ;
expression:
id op id {op id}
id is combination of digits and char, the first position contain a char.
I'm getting syntax error when I have something like this in my in.txt file: hellow = three3
but that shouldn't be a syntax error, and then when i put something like: hellow = =
that does not display a syntax error but it should. What am i doing wrong?
Lex:
%{
#include "y.tab.h"
#include <stdio.h>
%}
%%
[ \t\n]+ ;
[a-zA-Z][a-zA-Z0-9]* {
ECHO;
return ID;
}
%%
YACC:
%{
#include <stdio.h>
extern FILE * yyin;
%}
%token ID
%left '+' '-'
%left '*' '/' '%'
%right ';'
%%
assignment: expression
|
ID '=' expression
;
expression: ID
|
expression '*' expression
|
expression '/' expression
|
expression '%' expression
|
expression '+' expression
|
expression '-' expression
|
'(' expression ')'
;
%%
int main(void) {
yyin = fopen("in.txt", "r");
yyparse();
fclose(yyin);
return 0;
}
I can't say from just looking at this, but a good way to find out is running your parser when the environment variable YYDEBUG is 1, like in (unix):
YYDEBUG=1 ./a.out
You should get a detailed list of the steps the parser takes and the tokens it receives from the lexer (where the error is in most cases, like in your example: how does the parser ever get the '=' sign and the operators?). Compare this with the y.output file you get when you run yacc -v

can't find a simple error when parsing with YACC

i'm trying to make a very simple YACC parser on Pascal language which just includes integer declarations, some basic expressions and if-else statements. however, i cant find the error for hours and i'm going to be crazy soon. terminal says Error at line:0 but it is impossible!. i use flex and byacc for parser.i will be very glad if you can help me. this is my lex file as you can see;
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
extern int yylval;
int linenum=0;
%}
digit [0-9]
letter [A-Za-z]
%%
if return IF;
then return THEN;
else return ELSE;
for return FOR;
while return WHILE;
PROGRAM return PROGRAM_SYM;
BEGIN return BEGIN_SYM;
VAR return VAR_SYM;
END return END_SYM;
INTEGER return INTEGER_SYM;
{letter}({letter}|{digit})* return identifier;
[0-9]+ return NUMBER;
[\<][\=] return CON_LE;
[\>][\=] return CON_GE;
[\=] return CON_EQ;
[\:][\=] return ASSIGNOP;
; return semiColon;
, return comma;
\n {linenum++;}
. return (int) yytext[0];
%%
and this is my Yacc file
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
extern FILE *yyin;
extern int linenum;
%}
%token PROGRAM_SYM VAR_SYM BEGIN_SYM END_SYM INTEGER_SYM NUMBER
%token identifier INTEGER ASSIGNOP semiColon comma THEN
%token IF ELSE FOR WHILE
%token CON_EQ CON_LE CON_GE GE LE
%left '*' '/'
%left '+' '-'
%start program
%%
program: PROGRAM_SYM identifier semiColon VAR_SYM dec_block BEGIN_SYM statement_list END_SYM '.'
;
dec_block:
dec_list semiColon;
dec_list:
dec_list dec
|
dec
;
dec:
int_dec_list
;
int_dec_list:
int_dec_list int_dec ':' type
|
int_dec ':' type
;
int_dec:
int_dec comma identifier
|
identifier
;
type:
INTEGER_SYM
;
statement_list:
statement_list statement
|
statement
;
statement:
assignment_list
|
expression_list
|
selection_list
;
assignment_list:
assignment_list assignment
|
assignment
;
assignment:
identifier ASSIGNOP expression_list
;
expression_list:
expression_list expression semiColon
|
expression semiColon
;
expression:
'(' expression ')'
|
expression '*' expression
|
expression '/' expression
|
expression '+' expression
|
expression '-' expression
|
factor
;
factor:
identifier
|
NUMBER
;
selection_list:
selection_list selection
|
selection
;
selection:
IF '(' logical_expression ')' THEN statement_list ELSE statement_list
;
logical_expression:
logical_expression '=' expression
|
logical_expression '>' expression
|
logical_expression '<' expression
;
%%
void yyerror(char *s){
fprintf(stderr,"Error at line: %d\n",linenum);
}
int yywrap(){
return 1;
}
int main(int argc, char *argv[])
{
/* Call the lexer, then quit. */
yyin=fopen(argv[1],"r");
yyparse();
fclose(yyin);
return 0;
}
and finally i take an error at the first line when i give the input;
PROGRAM myprogram;
VAR
i:INTEGER;
i3:INTEGER;
j:INTEGER;
BEGIN
i := 3;
j := 5;
i3 := i+j*2;
i := j*20;
if(i>j)
then i3 := i+50+(45*i+(40*j));
else i3 := i+50+(45*i+(40*j))+i+50+(45*i+(30*j));
END.
For debugging grammars, YYDEBUG is your friend. Either stick #define YYDEBUG 1 in the %{..%} in the top of your .y file, or compile with -DYYDEBUG, and stick a yydebug = 1; in main before calling yyparse and you'll get a slew of info about what tokens the parser is seeing and what it is doing with them....
Your lexical analyzer returns blanks and tabs as tokens, but the grammar doesn't recognize them.
Add a parser rule:
[ \t\r] { }
This gets you to line 6 instead of line 0 before you run into an error. You get that error because you don't allow semicolons between declarations:
dec_block:
dec_list semiColon;
dec_list:
dec_list dec
|
dec
;
dec:
int_dec_list
;
That should probably be:
dec_block:
dec_block dec
|
dec
;
dec:
int_dec_list semiColon
;
Doing this gets you to line 14 in the input.
Incidentally, one of the first things I did was to ensure that the lexical analyzer tells me what it is doing, by modifying the rules like this:
if { printf("IF\n"); return IF; }
In long term code, I'd make that diagnostic output selectable at run-time.
You have a general problem with where you expect semicolons. It is also not clear that you should allow an expression_list in the rule for statement (or, maybe, 'not yet' — that might be appropriate when you have function calls, but allowing 3 + 2 / 4 as a 'statement' is not very helpful).
This grammar gets to the end of the input:
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
extern FILE *yyin;
extern int linenum;
%}
%token PROGRAM_SYM VAR_SYM BEGIN_SYM END_SYM INTEGER_SYM NUMBER
%token identifier INTEGER ASSIGNOP semiColon comma THEN
%token IF ELSE FOR WHILE
%token CON_EQ CON_LE CON_GE GE LE
%left '*' '/'
%left '+' '-'
%start program
%%
program: PROGRAM_SYM identifier semiColon VAR_SYM dec_block BEGIN_SYM statement_list END_SYM '.'
;
dec_block:
dec_block dec
|
dec
;
dec:
int_dec_list semiColon
;
int_dec_list:
int_dec_list int_dec ':' type
|
int_dec ':' type
;
int_dec:
int_dec comma identifier
|
identifier
;
type:
INTEGER_SYM
;
statement_list:
statement_list statement
|
statement
;
statement:
assignment
|
selection
;
assignment:
identifier ASSIGNOP expression semiColon
;
expression:
'(' expression ')'
|
expression '*' expression
|
expression '/' expression
|
expression '+' expression
|
expression '-' expression
|
factor
;
factor:
identifier
|
NUMBER
;
selection:
IF '(' logical_expression ')' THEN statement_list ELSE statement_list
;
logical_expression:
expression '=' expression
|
expression '>' expression
|
expression '<' expression
;
%%
void yyerror(char *s){
fprintf(stderr,"Error at line: %d\n",linenum);
}
int yywrap(){
return 1;
}
int main(int argc, char *argv[])
{
/* Call the lexer, then quit. */
yyin=fopen(argv[1],"r");
yyparse();
fclose(yyin);
return 0;
}
Key changes include removing assignment_list and expression_list, and modifying logical_expression so that the two sides of the expansion are expression, rather than the LHS being logical_expression (which then never had a primitive definition, leading to the problems with warnings).
There are still issues to resolve; the expression_list in the selection should be more restrictive to accurately reflect the grammar of Pascal. (You need a block where that could be a single statement or a BEGIN, statement list, END.)

Resources