Docsity

Evaluating Arithmetic Expressions: Seminar 1, Exercises of Theory of Formal Languages for Automata

Toggle sidebar
bg1
Seminar 1: Evaluating arithmetic expressions
February 2022
1 Evaluating expressions
Using quadruples for encoding elementary operations, determine how the fol-
lowing expression will be evaluated:
a·(b−c/d/e +f·g)/h −i
Starting point: you can encode c/d as (/, c, d, t1), which signifies that c/d =
t1.
2 Two-stack algorithm for expression evaluation
Dijkstra’s two-stack algorithm for evaluating expressions uses, as the name sug-
gests, two stacks: an operand stack (values) and an operator stack. Using the
algorithm, evaluate the following expressions:
a/b ·c−d·e+f
a·(b−c/d ·e)+(f−g·h)/i
3 Polish notation
In prefix form, the operator precedes the operands; so, for example, instead
of writing in infix form a+b, the prefix notation will be +ab. Similarly, for
b·(c−d), the prefix notation will be ·b−cd . Generate the prefix notation for
the following expression:
a·(b−c/d ·e)/f −g·h
1

Partial preview of the text

Download Evaluating Arithmetic Expressions: Seminar 1 and more Exercises Theory of Formal Languages for Automata in PDF only on Docsity!

Seminar 1: Evaluating arithmetic expressions

February 2022

1 Evaluating expressions

Using quadruples for encoding elementary operations, determine how the fol- lowing expression will be evaluated:

a · (b − c/d/e + f · g)/h − i Starting point: you can encode c/d as (/, c, d, t 1 ), which signifies that c/d = t 1.

2 Two-stack algorithm for expression evaluation

Dijkstra’s two-stack algorithm for evaluating expressions uses, as the name sug- gests, two stacks: an operand stack (values) and an operator stack. Using the algorithm, evaluate the following expressions:

a/b · c − d · e + f

a · (b − c/d · e) + (f − g · h)/i

3 Polish notation

In prefix form, the operator precedes the operands; so, for example, instead of writing in infix form a + b, the prefix notation will be +ab. Similarly, for b · (c − d), the prefix notation will be ·b − cd. Generate the prefix notation for the following expression:

a · (b − c/d · e)/f − g · h