龙书3.8答案

龙书3.8答案

Exercises for Section 3.8

出处:https://github.com/WaterH2P/Compilers-Principles

请添加图片描述

3.8.1

Suppose we have two tokens: (1) the keyword if, and (2)
id­entifiers, which are strings of letters other than if. Show:

  1. The NFA for these tokens, and
  2. The DFA for these tokens.
Answer

1、 NFA:
3-1
NOTE: this NFA has potential conflict, we can decide the matched lexeme by 1. take the longest 2. take the first listed.

2、DFA
请添加图片描述

3.8.2

Repeat Exercise 3.8.1 for tokens consisting of (1) the keyword while, (2) the keyword when, and (3) identifiers consisting of strings of letters and digits, beginning with a letter.

Answer

1、 NFA
请添加图片描述

2、 DFA
bother to paint

3.8.3 !

Suppose we were to revise the definition of a DFA to allow zero or one transition out of each state on each input symbol (rather than exactly one such transition, as in the standard DFA definition). Some regular expressions would then have smaller “DFA’s” than they do under the standard definition of a DFA. Give an example of one such regular expression.

Answer

Take the language defined by regular expression “ab” as the example, assume that the set of input symbols is {a, b}

1、Standard DFA

请添加图片描述
2、Revised DFA

请添加图片描述
Obviously, the revised DFA is smaller than the standard DFA.

3.8.4 !!

Design an algorithm to recognize Lex-lookahead patterns of
the form rl/r2, where rl and r2 are regular expressions. Show how your
algo­rithm works on the following inputs:

  1. (abcd|abc)/d
  2. (a|ab)/ba
  3. aa*/a*
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值