yacc $1 pass to variable in c? - yacc

I am working with lex and yacc, and I wonder how can I pass a value from $1 to a variable in c, in order to print it on the main?
file.y:
%{
#include <stdio.h>
%}
...
%token<num> NUM
%%
expression : NUM { printf("number:%d;\n", $1);}
;
%%
int main(){
yyparse();
printf("number is:%d;\n", var);
return 0;
}
doing something like '$1=var' to pass the value to the variable?

Related

Vowels Context free grammar definition YACC

I am writing a YACC program defining the CFG for vowels in the given string, My code attempt is as follows
%{
#include <stdio.h>
%}
%union{
char c;
}
%token <c> VOW
%%
cha : 'a' { printf("a\n"); }
| 'e' {printf("e\n");}
| 'i' {printf("i\n");}
| 'o' {printf("o\n");}
| 'u' {printf("u\n");}
;
%%
int main(void) {return yyparse();}
int yylex(void) {return getchar();}
void yyerror(char *s) {fprintf(stderr, "%s\n",s);}
Is this a correct definition of a CFG for vowels
You don't need a context-free grammar for your problem, only a regular expresion. You're using the wrong tool for the job. It is three lines in flex(1):
%%
[aeiou] printf("%\n", yytext);
.|\n ;

"first use" error when change the code in lex file

Given a .l file like this:
%{
#include "y.tab.h"
%}
%%
[ \t\n]
"if" return IF_TOKEN ;
"while" return ELSE_TOKEN ;
. yyerror("Invalid Character");
%%
int yywrap(void){
return 1;
}
and a .y file like this:
%{
#include <stdio.h>
void yyerror(char *);
%}
%token IF_TOKEN ELSE_TOKEN MINUS_TOKEN DIGIT_TOKEN
%%
program :expr {printf("program Accepted!!!");};
expr : IF_TOKEN | DIGIT_TOKEN ;
%%
void yyerror(char *s){
fprintf(stderr, "%s\n", s);
}
int main(){
yyparse();
return 0;
}
I use these 3 commands to compile these 2 files (my lex file named p.l and my yacc file named p.y):
flex p.l
yacc -d p.y
gcc lex.yy.c y.tab.c
It is compiled with no error. But when I changed "return ELSE_TOKEN" to "return WHILE_TOKEN", I got this error and got no output file:
p.l: In function ‘yylex’:
p.l:10:8: error: ‘WHILE_TOKEN’ undeclared (first use in this function)
"while" return WHILE_TOKEN ;
^
p.l:10:8: note: each undeclared identifier is reported only once for each function it appears in
Also when I change "while" to "else" and add a new rule like:
"for" return FOR_TOKEN ;
I get the same error. How can I correct the code to work correctly?
You didn't add:
%token WHILE_TOKEN FOR_TOKEN
to the grammar, so the header didn't contain a definition for WHILE_TOKEN or FOR_TOKEN, so the compilation of the lexical analyzer failed.

Runtime "syntax error" from lex and yacc

I cannot figure out why I am getting these results.
++
+add
+syntax error 2
++
+add
+syntax error 4
The ++ is my input and lex echoes each character and yacc prints add whenever it gets a +. It's giving me this error on every other + it gets. Doesn't matter how I give the input, I get the same results if I hit enter on every +.
lex
%{
#include "y.tab.h"
int chars = 0;
%}
%%
"+" {ECHO; chars++; return ADD;}
. {ECHO; chars++;}
\n {ECHO;}
%%
yacc
%{
#include <stdio.h>
extern int chars;
void yyerror (const char *str) {
printf ("%s %d\n", str, chars);
}
%}
%token ADD
%%
symbol : ADD {printf ("add\n");}
;
%%
int main () {
while (1) {
yyparse ();
}
}
Your grammar only accepts a 'sentence' that consists of a single token, +. When you type a second +, you induce a syntax error; your grammar doesn't allow ADD followed by ADD. Your next token after the + must be EOF for the grammar to accept your input. (Because of the . and \n rules, you can type all sorts of other stuff at the code, but there can only be one + in the input.)

why calles function yyerror() in sample programm

curs.l :
%{
#include <stdlib.h>
#include "tree.c"
#include "yycurs.h"
%}
L [a-zA-Z_]
D [0-9]
D4 [0-3]
IDENTIFIER ({L})({L}|{D})*
INT4 {D4}+'q'
INT {D}+
%%
{IDENTIFIER} {return VARIABLE;}
%%
int yywrap(void){
return 0;
}
curs.y:
%{
#include stdio.h
void yyerror(char*);
int yylex(void);
%}
%token VARIABLE INTEGER
%%
var: VARIABLE {printf($1);};
%%
void yyerror(char *s){
fprintf(stderr, "11\n");
fprintf(stderr, "%s\n", s);
}
int main(void){
yyparse();
return 0;
}
when i run my compiled progrum, i have such result:
./curs
ff //I introduced
//result
ff //I introduced
11 //result
syntax error //result
evgeniy#evgeniy-desktop:~/documents/compilers$
Can anybody explain me, why there appears 'syntax error'?
Thanks in advance.
Your grammar defiles that a valid file consists of exactly one VARIABLE. To have more then one, you need to introduce a recursive rule.
%start vars
%%
var: VARIABLE {printf($1);};
vars: var
| vars var;
%%

Why am I getting conflicts: 1 shift/reduce

I'm new to bison and I'm getting a "conflicts: 1 shift/reduce" error. Can anyone shed some light on this?
Here's the y file.
test.y:
%{
#include <stdio.h>
#include <string.h>
#define YYERROR_VERBOSE
#define YYDEBUG 1
void yyerror(const char *str);
int yywrap();
%}
%union
{
int integer;
char *string;
}
%token <string> VAR_LOCAL
%token <integer> LIT_NUMBER
%token <string> LIT_STRING
%token WS_LINEBRK
//%token SYMB_EQL
%token SYMB_PLUS
%token SYMB_MINUS
%token SYMB_MUL
%token SYMB_DIV
%%
/*
// Sample input
num = 10
str = "this is a string"
*/
inputs: /* empty token */
| literal
| variable
| inputs stmt WS_LINEBRK
;
stmt: variable "=" exps
;
exps: variable op literal
| variable op variable
| literal op literal
| literal op variable
;
op: SYMB_PLUS | SYMB_MINUS | SYMB_MUL | SYMB_DIV ;
variable: VAR_LOCAL
{
printf("variable: %s\n", $1);
}
;
literal:
number | string
;
string: LIT_STRING
{
printf("word: %s\n", $1);
}
;
number: LIT_NUMBER
{
printf("number: %d\n", $1);
}
;
%%
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
Here's the lex file
test.l:
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
int line_no = 0;
%}
%%
[a-z][a-zA-Z0-9]* {
// local variable
yylval.string=strdup(yytext);
return VAR_LOCAL;
}
[0-9]+ {
//number literal
yylval.integer=atoi(yytext);
return LIT_NUMBER;
}
= return SYMB_EQL;
\+ return SYMB_PLUS;
\- return SYMB_MINUS;
\* return SYMB_MUL;
\/ return SYMB_DIV;
\"[-+\!\.a-zA-Z0-9' ]+\" {
// word literal
yylval.string=strdup(yytext);
return LIT_STRING;
}
\n {
// line break
printf("\n");
return WS_LINEBRK;
}
[ \t]+ /* ignore whitespace */;
%%
bison -r test.y will write a file test.output with a detailed description of the generated state machine that allows you to see what's going on - such as the state where the shift/reduce conflict occurs.
In your case, the problem is in the start state (corresponding to your start nonterminal, inputs). Say the first token is VAR_LOCAL. There's two things your parser could do:
It could match the variable case.
It could also match the inputs stmt WS_LINEBRK case: inputs matches the empty string (first line), and stmt matches variable "=" exps.
With the one token of lookahead that bison parsers use, there's no way to tell. You need to change your grammar to get rid of this case.
To fix the grammar, as Fabian has suggested, move the variable and literal to the end of exps from inputs
inputs:
| variable
| literal
exps:
...
| variable
| literal
That allows x= y,x="aliteral" syntax.
To allow for empty input lines, change the /* empty token */ rule to WS_LINEBREAK:
inputs: WS_LINEBRK
| stmt WS_LINEBRK
| inputs stmt WS_LINEBRK
;
On another note, since the scanner still looks for the SYMB_ EQUAL ; but the parser no longer defines it (its commented out), something needs to be done in order to compile. One option is to uncomment the %token definition and use SYMB_ EQUAL instead of the literal "=" in the parser .y file.