edited by
1,347 views
4 votes
4 votes

The language $L,$ defined by the following grammar, allows use of real or integer data in expressions and assignment statements.

<assign-stmt> :: <LHS> := <E>
          <E> ::= <E> + <T>|<T>
          <T> ::= <T> * <V>|<V>
          <V> ::= id|(<E>)
        (LHS) :: id

It is required to convert expression and assignment string of $L$ into postfix strings that use the type-specific operations $(+,i),(+,r),(*,i),(*,r),(:=,i)$ and $(:=,r)$
Write a syntax-directed translation scheme to convert expression and assignment strings into the postfix form. You may assume that the name and type of variable can be obtained by making the function calls’ give_name $(id)$ and give_type $(id)$ respectively. 

edited by

1 Answer

0 votes
0 votes

Here's a possible translation scheme:

\begin{align*} 1. & \quad \text{<assign-stmt>} \ ::= \ \text{<LHS> := <E>} \quad & \{ \text{postfix(LHS, E);} \} \\ 2. & \quad \text{<E>} \ ::= \ \text{<E> + <T> | <T>} \quad & \{ \text{postfix(E, T, "+");} \} \\ 3. & \quad \text{<T>} \ ::= \ \text{<T> * <V> | <V>} \quad & \{ \text{postfix(T, V, "*");} \} \\ 4. & \quad \text{<V>} \ ::= \ \text{id | (<E>)} \quad & \{ \text{postfix(V);} \} \\ 5. & \quad \text{(LHS)} \ ::= \ \text{id} \quad & \{ \} \\ \end{align*}

 

The translation scheme defines the following semantic actions:

  1. Assignment Statement (<assign-stmt>):

    • Postfix the right-hand side expression (E), then append the assignment operation (:=) with the appropriate type information.
  2. Expression Addition (<E>):

    • Postfix the left-hand side expression (E), postfix the right-hand side term (T), and append the addition operation (+) with the appropriate type information.
  3. Term Multiplication (<T>):

    • Postfix the left-hand side term (T), postfix the right-hand side variable (V), and append the multiplication operation (*) with the appropriate type information.
  4. Variable (<V>):

    • If the production is id, postfix the identifier.
    • If the production is (<E>), postfix the expression inside the parentheses.
  5. Left-Hand Side ((LHS)):

    • For a variable identifier (id), do nothing.

The type-specific operations ((+,i), (+,r), (*,i), (*,r), (:=,i), (:=,r)) are determined based on the type information obtained from the give_type function for each variable.

The translation scheme generates postfix expressions for expressions and assignments in the given language with type-specific operations.

Related questions

5 votes
5 votes
0 answers
1
Kathleen asked Oct 5, 2014
1,002 views
Suppose we have a computer with single register and only three instructions given below:$$\begin{array}{ll} \text{LOAD addren} & \text{; load register} \\ \text{} & \te...