加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何debug bison和flex写的程序

发布时间:2020-12-15 04:48:02 所属栏目:百科 来源:网络整理
导读:具体的code请参见:http://www.voidcn.com/article/p-rodfeair-bkh.html 只不过有两步需要修改: bison -y -d parser.l -- bison -y -d -t parser.l gcc y.tab.o lex.yy.o -o plusmins.exe --gcc -g lex.yy.o y.tab.o -o plusminis.exe 以下是debug的屏幕输

具体的code请参见:http://www.voidcn.com/article/p-rodfeair-bkh.html

只不过有两步需要修改:

bison -y -d parser.l --> bison -y -d -t parser.l

gcc y.tab.o lex.yy.o -o plusmins.exe -->gcc -g lex.yy.o y.tab.o -o plusminis.exe

以下是debug的屏幕输出:

gdb plusminis.exe
...
Breakpoint 1,main () at parser.l:30
30????????? yyparse();
(gdb) set yydebug=1 //使debug生效,也可以将yydebug=1放到main函数中去
(gdb) cont
Continuing.
Starting parse
Entering state 0 //进入状态0
Reducing stack by rule 2 (line 14):
-> $$ = nterm program ()
Stack now 0
Entering state 1 //进入状态1
Reading a token: 3 //等待用户输入,输入是3
Next token is token INTEGER ()
Shifting token INTEGER () //压栈
Entering state 3 //进入状态3
Reducing stack by rule 3 (line 18):
?? $1 = token INTEGER ()
-> $$ = nterm expr () //根据rule 3 expr: INTEGER,将INTEGER替换成expr
Stack now 0 1 //替换完成后,栈中的状态是0,1
Entering state 4 //进入状态4
Reading a token: Next token is token 'n' ()
Shifting token 'n' ()
Entering state 5 //进入状态5,栈中的状态是0,1,4,
Reducing stack by rule 1 (line 13):
?? $1 = nterm program ()
?? $2 = nterm expr ()
?? $3 = token 'n' ()
3
-> $$ = nterm program () //根据rule 1 program: program expr 'n',将 program expr 'n '替换成program,同时输出expr
Stack now 0
Entering state 1

----------------------------
Reading a token: 3+3
Next token is token INTEGER ()
Shifting token INTEGER ()
Entering state 3
Reducing stack by rule 3 (line 18):
?? $1 = token INTEGER ()
-> $$ = nterm expr ()
Stack now 0 1
Entering state 4
Reading a token: Next token is token '+' ()
Shifting token '+' ()
Entering state 6
Reading a token: Next token is token INTEGER ()
Shifting token INTEGER ()
Entering state 3
Reducing stack by rule 3 (line 18):
?? $1 = token INTEGER ()
-> $$ = nterm expr ()
Stack now 0 1 4 6 //这里其实是状态机中的状态的入栈情况
Entering state 8
Reading a token: Next token is token 'n' ()
Reducing stack by rule 4 (line 19):
?? $1 = nterm expr ()
?? $2 = token '+' ()
?? $3 = nterm expr ()
-> $$ = nterm expr () //根据rule 4 expr '+' expr?????????? { $$ = $1 + $3; },会更新栈顶的expr值
Stack now 0 1
Entering state 4
Next token is token 'n' ()
Shifting token 'n' ()
Entering state 5
Reducing stack by rule 1 (line 13):
?? $1 = nterm program ()
?? $2 = nterm expr ()
?? $3 = token 'n' ()
6
-> $$ = nterm program ()
Stack now 0
Entering state 1

-------------------------

Reading a token: s
invalid character
Next token is token 'n' ()
syntax error
Error: popping nterm program ()
Stack now 0
Cleanup: discarding lookahead token 'n' ()
Stack now 0

Program exited normally.
(gdb) quit


使用bison -y -d -t -v parser.l产生的状态机parser.output

State 8 conflicts: 2 shift/reduce
State 9 conflicts: 2 shift/reduce


Grammar

    0 $accept: program $end

    1 program: program expr 'n'
    2        | /* empty */

    3 expr: INTEGER
    4     | expr '+' expr
    5     | expr '-' expr


Terminals,with rules where they appear

$end (0) 0
'n' (10) 1
'+' (43) 4
'-' (45) 5
error (256)
INTEGER (258) 3


Nonterminals,with rules where they appear

$accept (7)
    on left: 0
program (8)
    on left: 1 2,on right: 0 1
expr (9)
    on left: 3 4 5,on right: 1 4 5


state 0

    0 $accept: . program $end

    $default  reduce using rule 2 (program)

    program  go to state 1


state 1

    0 $accept: program . $end
    1 program: program . expr 'n'

    $end     shift,and go to state 2
    INTEGER  shift,and go to state 3

    expr  go to state 4


state 2

    0 $accept: program $end .

    $default  accept


state 3

    3 expr: INTEGER .

    $default  reduce using rule 3 (expr)


state 4

    1 program: program expr . 'n'
    4 expr: expr . '+' expr
    5     | expr . '-' expr

    'n'  shift,and go to state 5
    '+'   shift,and go to state 6
    '-'   shift,and go to state 7


state 5

    1 program: program expr 'n' .

    $default  reduce using rule 1 (program)


state 6

    4 expr: expr '+' . expr

    INTEGER  shift,and go to state 3

    expr  go to state 8


state 7

    5 expr: expr '-' . expr

    INTEGER  shift,and go to state 3

    expr  go to state 9


state 8

    4 expr: expr . '+' expr
    4     | expr '+' expr .
    5     | expr . '-' expr

    '+'  shift,and go to state 6
    '-'  shift,and go to state 7

    '+'       [reduce using rule 4 (expr)]
    '-'       [reduce using rule 4 (expr)]
    $default  reduce using rule 4 (expr)


state 9

    4 expr: expr . '+' expr
    5     | expr . '-' expr
    5     | expr '-' expr .

    '+'  shift,and go to state 7

    '+'       [reduce using rule 5 (expr)]
    '-'       [reduce using rule 5 (expr)]
    $default  reduce using rule 5 (expr)

refs: http://www.gnu.org/software/bison/manual/html_node/Understanding.html

???????? http://epaperpress.com/lexandyacc/pry1.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读