LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2011, 04:31 AM   #1
Yogesh Bhavsar
LQ Newbie
 
Registered: Feb 2010
Location: Malegaon/ Pune
Distribution: Ubuntu 9.04
Posts: 20

Rep: Reputation: 0
Problems in Yacc C Grammar


I am designing a parser for C language in Lex and Yacc. I have also found the references. Those are as follows:
Lex File: http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
Yacc File: http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

When I Compile these two files, there is a shift/reduce conflict produced. I tried to find the rule due to which the conflict is arising, but failed. Can anyone tell me where is it?

There is another problem:
When I give the following file as an input program:
int main(){
int c, d;
c=3;
}
I have written a printf statement in the rule
primary_expression: IDENTIFIER { printf("%s",yytext)};
at the time of running the program, it prints the c,d in the first line, but it doesn't print c from the second line. Instead it prints the character '='. Please help me with this...
 
Old 01-11-2011, 06:23 AM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Regarding the shift-reduce conflict, this is the location:

Code:
selection_statement
	: IF '(' expression ')' statement
	| IF '(' expression ')' statement ELSE statement
This is a commonly known shift-reduce conflict in yacc/bison grammars. But this nevertheless works correctly, though you could suppress this conflict via the 'expect' directive.

Andi
 
Old 01-12-2011, 03:12 AM   #3
Yogesh Bhavsar
LQ Newbie
 
Registered: Feb 2010
Location: Malegaon/ Pune
Distribution: Ubuntu 9.04
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ForzaItalia2006 View Post
Regarding the shift-reduce conflict, this is the location:

Code:
selection_statement
	: IF '(' expression ')' statement
	| IF '(' expression ')' statement ELSE statement
This is a commonly known shift-reduce conflict in yacc/bison grammars. But this nevertheless works correctly, though you could suppress this conflict via the 'expect' directive.

Andi
Thank you. Can you tell me something about my second problem also? That is freaking me out!
 
Old 01-13-2011, 03:54 PM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by Yogesh Bhavsar View Post
primary_expression: IDENTIFIER { printf("%s",yytext)};
at the time of running the program, it prints the c,d in the first line, but it doesn't print c from the second line. Instead it prints the character '='. Please help me with this...
Okay, I think I now found your error. You cannot use yytext outside of your scanner. Here's a nice quote from the flex manual:

Quote:
In short, the value of yytext is only valid from within the matched rule’s action.
Though, flex is free to possible re-assign 'yytext' to the next token's string in the input stream. That's why you see the '=' character. The correct way to do this is to define a union in your parser and then reference it via yyval (or similar if you choose to change the yy prefix) in the scanner. You then need to do a strdup(yytext) just because the original 'yytext' is not valid outside of the action. For details, please check out the flex and bison manuals and your favorite web search. There's already a bunch of examples out there how to do this ...

Hope that helps,
Andi
 
Old 01-24-2011, 04:58 AM   #5
Yogesh Bhavsar
LQ Newbie
 
Registered: Feb 2010
Location: Malegaon/ Pune
Distribution: Ubuntu 9.04
Posts: 20

Original Poster
Rep: Reputation: 0
Sorry for late reply... there was a problem of connectivity... I will test that now...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with English? Questions? Vocabulary, grammar... Post here :) frenchn00b General 1225 02-12-2017 04:52 AM
Yacc conflicts - How to understand grammar conflict dayalan_cse Programming 6 06-15-2010 08:04 AM
yacc grammar and its corresponding lex specification for designing C Compiler aashish Linux - Software 4 10-14-2008 02:22 AM
C and Yacc problems alltime Programming 2 06-30-2006 12:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration