LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-13-2006, 11:37 AM   #1
russinoff
LQ Newbie
 
Registered: Jun 2006
Posts: 1

Rep: Reputation: 0
Yacc/Bison question


According to my naive understanding of the Yacc/Bison algorithm
(derived from a cursory reading of the "dragon book"), I would expect
the parser generated from the grammar

p: a;
p: 'b';
a: p;

to execute an infinite loop on the following input:

b b

This conjecture is supported by the "output" file that my version of
Bison writes for this grammar, which lists the following states
and transitions:

state 0

'b' shift, and go to state 1

p go to state 2
a go to state 3



state 1

p -> 'b' . (rule 2)

$default reduce using rule 2 (p)



state 2

a -> p . (rule 3)

$ go to state 4

$ [reduce using rule 3 (a)]
$default reduce using rule 3 (a)



state 3

p -> a . (rule 1)

$default reduce using rule 1 (p)



state 4

$ go to state 5



state 5

$default accept


However, it turns out that this is not an accurate description of the
state machine that Bison actually produces. According to what I see in
the "tab.c" file, the default in state 2 is an error (instead of a
reduction), and the loop is thereby broken. This is confirmed by
execution:

Starting parse
Entering state 0
Reading a token: Next token is 98 ('b')
Shifting token 98 ('b'), Entering state 1
Reducing via rule 2 (line 36), 'b' -> p
state stack now 0
Entering state 2
Reading a token: Next token is 98 ('b')
(line 1) parse error


Apparently, Bison has some method more avoiding loops of this sort,
which is not discussed in the dragon book and not reflected in the
output file. Can anyone tell me exactly how this works?
 
Old 06-17-2006, 01:54 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi. Your grammar can not produce sentence 'bb'. I think this is the problem. Try grammar like this:
p: 'b' p | 'b'
 
  


Reply



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
bison/fleq question ruudmeer Linux - Software 0 01-17-2006 05:10 AM
Bison and Yacc Compatibility Problem oulevon Programming 1 10-23-2005 10:56 PM
another flex & yacc question sibtay Programming 1 12-25-2004 10:34 PM
an yacc error message question feetyouwell Programming 1 10-16-2004 04:33 PM
yacc, Bison Config Linux - General 6 02-21-2002 02:18 PM

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

All times are GMT -5. The time now is 11:55 AM.

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
Open Source Consulting | Domain Registration