0% found this document useful (0 votes)
22 views4 pages

PU Paper 2022

The document describes a translation scheme to convert infix arithmetic expressions to prefix notation using a stack. The scheme involves initializing an empty stack, reversing and tokenizing the infix expression, then processing each token - pushing operands and operators to the stack or adding to the output based on the token type.

Uploaded by

Zubair Jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

PU Paper 2022

The document describes a translation scheme to convert infix arithmetic expressions to prefix notation using a stack. The scheme involves initializing an empty stack, reversing and tokenizing the infix expression, then processing each token - pushing operands and operators to the stack or adding to the output based on the token type.

Uploaded by

Zubair Jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Paper – 2022

Question #4:

In compiler construction, a translation scheme is a set of rules or instructions used to


convert one language or notation into another. To convert an infix arithmetic expression
to prefix notation, we can use a translation scheme that utilizes a stack to keep track of
operators and operands. Here's a translation scheme for this purpose:

Translation Scheme:

1. Input: Infix arithmetic expression.


2. Output: Equivalent prefix notation.

Grammar Rules:

 Let E be the start symbol representing an arithmetic expression.


 Let E -> E + T | E - T | T represent the rules for addition and subtraction.
 Let T -> T * F | T / F | F represent the rules for multiplication and division.
 Let F -> (E) | id represent the rules for parentheses and operands.

Translation Rules:

1. Initialize an empty stack S.


2. Reverse the infix expression and tokenize it into a list of symbols.
3. For each symbol s in the list:
 If s is an operand, add it to the output.
 If s is a right parenthesis ), push it onto S.
 If s is an operator:
 While S is not empty and the precedence of the operator at the top of S is greater than
or equal to the precedence of s, pop operators from S and add them to the output.
 Push s onto S.
 If s is a left parenthesis (, pop operators from S and add them to the output until a
matching right parenthesis is encountered. Pop and discard the right parenthesis.
4. Pop any remaining operators from S and add them to the output.
5. Reverse the output to obtain the final prefix notation.
Example:

Consider the infix expression: A + B * C - D

1. Reverse the infix expression: D - C * B + A


2. Tokenize the reversed infix expression: [D, -, C, *, B, +, A]
3. Initialize stack S and output list O.
4. Process each symbol in the list:
 Push D onto S.
 Push - onto S.
 Push C onto S.
 Push * onto S.
 Push B onto S.
 Pop B, *, C from S and add to O: [B, C, *]
 Push + onto S.
 Push A onto S.
 Pop A, + from S and add to O: [A, B, C, *, +]
 Pop -, D from S and add to O: [A, B, C, *, +, D, -]
5. Reverse O to obtain the final prefix notation: [-, +, A, *, B, C, D]

Therefore, the prefix notation for the infix expression A + B * C - D is - + A * B C D


Q#8: Give the attributed grammar which can evaluate the [attribute numeric value] of
floating point strings which can be generated by following CFG: R->N.N N->N D N->D D-
>0|1|2|3|4|...|9
To evaluate the numeric value of floating-point strings generated by the given context-free grammar
(CFG), we can use an attributed grammar where each non-terminal has an attribute val that
represents the numeric value of the string derived from that non-terminal. We can define the
attribute val for each production rule. Here is the attributed grammar:

Attributed Grammar:

1. Input: Floating-point string generated by the CFG.


2. Output: Numeric value of the floating-point string.

Grammar Rules:

 Let R be the start symbol representing a floating-point string.


 Let N be a non-terminal representing an integer part of the floating-point string.
 Let D be a non-terminal representing a digit.
 Let val be the attribute representing the numeric value of a string.

Production Rules with Attribute Calculations:

1. R -> N . N { R.val = N1.val + N2.val / 10^k }


2. N -> N D { N.val = N1.val * 10 + D.val }
3. N -> D { N.val = D.val }
4. D -> 0 { D.val = 0 }
5. D -> 1 { D.val = 1 }
6. D -> 2 { D.val = 2 }
7. D -> 3 { D.val = 3 }
8. D -> 4 { D.val = 4 }
9. D -> 5 { D.val = 5 }
10. D -> 6 { D.val = 6 }
11. D -> 7 { D.val = 7 }
12. D -> 8 { D.val = 8 }
13. D -> 9 { D.val = 9 }

In this attributed grammar, the attribute val is calculated for each non-terminal and terminal symbol
based on the production rules. The val attribute for a floating-point string (R) is calculated as the
sum of the integer part (N1) and the fractional part (N2) divided by 10 raised to the power of the
number of digits in the fractional part ( k).
2. Semantic Rules:
 R.val represents the numeric value of the floating-point string generated by the
production R -> N.N.
 N.val represents the numeric value of the integer part of the floating-point string
generated by the production N -> N1 D.
 N.val1 and N.val2 represent the numeric values of the integer parts of the left and
right sides of the decimal point in the floating-point string generated by the production
R -> N.N.
 D.val represents the numeric value of a single digit in the floating-point string
generated by the productions N -> D and D -> 0|1|2|3|4|...|9.

Example:

Consider the floating-point string 123.456.

Using the attributed grammar:

R -> N.N { R.val = N.val1 + N.val2 / 10.0 }


N -> N1 D { N.val = N1.val * 10.0 + D.val }
N -> D { N.val = D.val }
D -> 1 { D.val = 1.0 }
D -> 2 { D.val = 2.0 }
D -> 3 { D.val = 3.0 }
D -> 4 { D.val = 4.0 }
D -> 5 { D.val = 5.0 }
D -> 6 { D.val = 6.0 }

Parsing the string 123.456:

R.val = N.val1 + N.val2 / 10.0 = 123.0 + 45.6 / 10.0 = 123.0 + 4.56 = 127.56

Therefore, the numeric value of the floating-point string 123.456 is 127.56.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy