Translation Program - 20


Use the push-pull parsing algorithm to parse each input string w; then construct its parse tree if it exists:

1) w = id - (id + id).

2) w = (id + id) / id.

3) w = (id + id)- (id.

4) w = id + * (id - id) / id.

5) w = (id + id) / (id + id).

6) w = id * id - id) + id.

7) w = id * (id + id)/(id - id).

3.46. Given a grammar with the following production rules:

1. E E+T ;

2. E T ; 3.T T*F ; 4.T F ;

5. F (E) ;

6. F id.

With the parse table being:


Status

Action

Goto

id

+

*

(

)

$

E

T

F

0

s 5



s 4



1

2

3

1


s 6




account




2


r 2

s 7


r 2

r 2




3


r 4

r 4


r 4

r 4




4

s 5



s 4



8

2

3

5


r 6

r 6


r 6

r 6




6

s 5



s 4




9

3

7

s 5



s 4





10

8


s 6



s 11





9


r 1

s 7


r 1

r 1




10


r 3

r 3


r 3

r 3




11


r 5

r 5


r 5

r 5




Maybe you are interested!

Translation Program - 20

BT 3.464.6 SLR Parsing Table


Use the LR parsing algorithm to parse the input string:

1) w = id * (id + id).

2) w = id + id + id).

3) w = id + * id + id.

4) w = id + (id + id).

5) w = (id + (id + id))* id.

6) w = (id + id) * (id + id).

7) w = id * ((id + id)* id).

3.47. Given a grammar that generates mathematical expressions with the generation rules: E E * T | T;

T T + F | F ;

F F / G | G ; G G - B | B;

B → ( E ) | id .

1) Construct the LR(0) item set family of the grammar.

2) Build action and goto functions for the SLR parser of the grammar.

3) Build an SLR parser table for the grammar.

4) Use the parsing algorithm to parse the input string:

1) w = id - (id + id).

2) w = (id + id) / id.

3) w = (id + id) - (id.

4) w = id + * (id - id) / id.

5) w = (id + id) / (id + id).

6) w = id * id - id) + id.

7) w = id * (id + id)/(id - id).


SUMMARY SOLUTION OR GUIDE


CHAPTER 1

1.9. Summary solution

1. digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Then: + digit is a token;

+ 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 is the vocabulary pattern of the digit card;

+ Each digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 is a lexeme of the digit card.

2. digits digit(digit) *

Then: + digits is a token;

+ The regular expression digit(digit) * is the pattern of the digits tag;

+ Each number: 0, 1, 20, 135, ... is a lexeme of the digits tag.

3. optional_fraction . digits | ε

Then: + optional_fraction is a token;

+ The regular expression . digits | ε is the lexical pattern of the optional_fraction tag;

+ Each: ε, .0, .5, .21, .135, ... is a lexeme of the optional_fraction tag.

4. optional_exponent ( E ( + | - | ε ) digits) | ε Then: + optional_exponent is a token;

+ Regular expression ( E ( + | - | ε ) digits) | ε is the lexical pattern of the optional_exponent tag;

+ Each: ε, E-1, E5, E+2, E135, ... is a lexeme of the optional_exponent tag.

5. nums digits optional_fraction optional_exponent Then: + nums is a token;


+ Regular expression: digits optional_fraction optional_exponent is the lexical pattern of the num tag;

+ Each: 1, 126, 572E-1, 329.22 E5, 143.0 E+2, ... is a lexeme of the nums tag.

1.10. Summary solution

a) With the high-level programming language Pascal

1. Data type

- Integer

digit 0 | 1 |...| 9 ; sign + | - | ε ;

*

digits (sign)(digit)(digit)

- Short integer (sortint)

- Long integer (longint)

- Real number

+ Fixed floating point real number optional_fraction . digits | ε ;

number (sign)(digits)(optional_fraction)

+ Floating point numbers

optional_exponent ( E ( + | - | ε ) digits) | ε;

nums digits optional_fraction optional_exponent.

- Character (Char) – alphabet

Char A | B | ...| Z | a | b |...| z | 0 | 1|...| 9| ( | ) | [ | ] | { | }| ?| < | -| +| ...

- String

*

String (char)(char)

- logical (boolean)

Boolean true | false

...

2. Relational operation (Relop) relop < | <= | > | >= | <>

3. Arithmetic operation (operator)


Operator+-*/**

4. Assignment Assign :=

5. Name (Identifer)

letter A | B | ...| Z | a | b |...| z ; digit 0 | 1 | ...| 9 ;

id (letter| _ ) (letter | digit | _ ) *

6. Standard identifier

Integer Integer; Shortint Shortint; longint longint; real real;

Char Char; String String; Boolean Boolean; pi 3.141...;

There are also many other standard names such as: sqr, sqrt, copy, val, max, min, ...

7. Keyword

If if;

then then; else else; while while; due to due to;

to to; for for;

repeat repeat; until until; program program; label label;


const const; var var;

function function; procedure procedure; begin begin

end end

8. Signs ,

- semi ;

- comma ,

- Dot .

- twodot :

...

9. Space, tab, newline delim blank | tab | newline ;

+

ws delim .

10. othersign (, ), …

b) With high-level programming language C: Similar


CHAPTER 2

2.10. Summary solution


Status

Token

Lexeme

1

function

function

2

begin

begin

3

if

if

4

then

then

5

else

else

6

end

end

7

id

max2, i , j


8

integer

Integer

9

Assign

: =

10

twocham

:

11

relop

>

12

semi

;


Table BT 2.10.

2.11. Summary solution


Status

Token

Lexeme

1

PROGRAM

PROGRAM

2

VAR

VAR

3

FUNCTION

FUNCTION

4

Begin

Begin

5

End

End

6

PROCEDURE

PROCEDURE

7

REPEAT

REPEAT

8

UNTIL

UNTIL

9

If

If

10

Then

Then

11

Id

GPTB2, a, b, c, x1, x2, Delta, Ptb2, r

12

Semi

;

13

Comma

,

14

Twocham

:

15

Real

real

16

Assign

:=

17

Sqr

Sqr

18

Sqrt

Sqrt

19

Write

Write

20

WriteIn

WriteIn

21

readln

readln



22

Operator

-, +, *, /,

23

othersign

(, )

24

Litral

„a=‟, „b=‟, „c=‟, „inexplicable equation‟, „double-solved equation x1 = x2 =‟, „x2 =‟,

The equation has two solutions: x1=

25

relop

<>, <, >, =,

26

digits

0, 1, 2

27

dot

.

Table BT 2.11.


2.12. Summary solution

a) Represent each word card by a formal definition and indicate the word card, vocabulary pattern, and vocabulary value of each word card.

1) digit 0 | 1 |...| 9 ; digits (digit)(digit) *

2) digit 0 | 1 |...| 9 ; sign + | - | ε ;

digits (sign)(digit)(digit) *

3) digit 0 | 1 |...| 9 ; sign + | - | ε ;

digits (sign)(digit)(digit) *

optional_fraction . digits | ε ;

number (sign)(digits)(optional_fraction) .

4) digit 0 | 1 |...| 9 ; sign + | - | ε ;

digits (sign)(digit)(digit) *

optional_fraction . digits | ε ; optional_exponent ( E ( + | - | ε ) digits) | ε;

nums digits optional_fraction optional_exponent.

Comment


Agree Privacy Policy *