0% found this document useful (0 votes)
26 views21 pages

cs480 091411

The document discusses ambiguity in expressions and how it can be resolved. It covers operator precedence and associativity, and provides examples of parsing expressions and generating bytecode instructions for different programming languages.

Uploaded by

cecsdistancelab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views21 pages

cs480 091411

The document discusses ambiguity in expressions and how it can be resolved. It covers operator precedence and associativity, and provides examples of parsing expressions and generating bytecode instructions for different programming languages.

Uploaded by

cecsdistancelab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Eliminating Ambiguity

<expr> <term> + <expr> <expr> <term> <term> <unit> * <term> <term> <unit> <unit> num <expr> <expr> <unit> ( <expr> )

<expr> <op> <expr> ( <expr> ) <expr> num <op> + <op> *

cs480(Prasad)

L2Syntax

Extended BNF
<expr> <term> ( + <term> ) * <term> <unit> ( * <unit> ) * <unit> num | ( <expr> )
<expr> <expr> <expr> <op> <expr> ( <op> <expr> ) * ( <expr> ) num +|*

cs480(Prasad)

L2Syntax

Expression Parser Fragment


Node expr() { // PRE: Expects lookahead token. // POST: Consume an Expression // and update Lookahead token. Node temp = term(); while ( inTok.ttype == '+') { inTok.nextToken(); Node temp1 = term(); temp = new OpNode(temp,'+',temp1); } return temp; }

cs480(Prasad)

L2Syntax

Arithmetic Expressions (with variables)

(ambiguous)

<expr> <expr> + <expr> | <expr> * <expr> | ( <expr> ) | <variable> | <constant> <variable> <constant>
cs480(Prasad)

x|y|z 0|1|2
L2Syntax 4

Resolving Ambiguity in Expressions


Different operators : precedence relation Same operator : associativity relation
5-2-3
5-2 - 3 =0 5- 2-3 =6 2**3**4 2^12 2^81

Left Associative ( (5-2)-3)


cs480(Prasad)

Right Associative (2**(3**4))


L2Syntax 5

C++ Operator Precedence and Associativity


Level 17R 17L 16L 16L 16L 16L 15R 15R 15R 15R 15R 15R 15R 15R 14L 13L Operator :: :: -> , . [] () () sizeof ++ , -~ ! +,*,& () new , delete ->* , .* *,/,% Function global scope (unary) class scope (binary) member selectors array index function call type construction size in bytes increment, decrement bitwise NOT logical NOT uniary minus, plus dereference, address-of type conversion (cast) free store management member pointer select multiplicative operators Level 12L 11L 10L 9L 8L 7L 6L 5L 4L 3L 2R Operator Function +,arithmetic operators << , >> bitwise shift < , <= , > , >= relational operators == , != equaltity, inequality & bitwise AND ^ bitwise XOR | bitwise OR && logical AND || logical OR ?: arithmetic if = , *= , /= , %= , += , -= <<= , >>= , &= , |= , ^= assignment operators , comma operator

1L

cs480(Prasad)

L2Syntax

Assignment 2 Solution Sketch


Translation to MSIL and Javabyte Code

Constant Expression : Infix to postfix


2+3*4 ( 2 + (3 * 4 ) ) 2 3 4*+

Evaluating postfix expression using stack


|2| |2|3|4| | 2 | 12 | |14|

Evaluating postfix expression using stack


|2| |2|3|4| | 2 | 12 | |14|

Compiling constant expression for a stack machine


Push 2 Push 3 Push 4 Mul Add

Generalizing to expressions with variables i+j*k


Push i Push j Push k Mul Add

Conversion to abstract syntax tree + i * j k

Generalizing to expressions with variables i+j*k


Push i Push j Push k Mul Add

Byte code generation for static f(int i,j,k)


ldarg.0 ldarg.1 ldarg.2 mul add

Byte code for i + j + k for static f(int i,j,k)


Right associative +
ldarg.0 ldarg.1 ldarg.2 add add

Left associative +
ldarg.0 ldarg.1 add ldarg.2 add

Introducing numeric types with real variables a+b


Push a Push b Add

Byte code generation for static f(double a,b)


ldarg.0 ldarg.1 add

Mixing int and double variables (requiring coercion code) for static f(double a,int i, j) i+j*a
ldarg.1 conv.r8 ldarg.2 conv.r8 ldarg.0 mul add

Translation algorithm essence trans (e1 * e2) = trans(e1) [type coercion code?] trans(e2) [type coercion code?] trans(*) Map grammar rules to control structures
E.g., alternatives, while-loop, etc

Mixing int and double variables (requiring coercion code) for static f(double a,int i, j) i+j*a
iload_2 i2d iload_3 i2d dload_0 dmul dadd

Object-Oriented Programming
Programming with Data Types to enhance reliability and productivity (through reuse and by facilitating evolution)

CS480 (Prasad)

L3OOP

17

Object (instance)
State (fields) Behavior (methods) Identity

Data Abstraction Modularity Encapsulation

Class
code describing implementation of an object

Inheritance Polymorphism

CS480 (Prasad)

L3OOP

18

Abstraction
General: Focus on the meaning
Suppress irrelevant implementation details

Programming Languages :
Assign names to recurring patterns
Value Expression Statements Control Value/ops
CS480 (Prasad)

: constant identifier : function : procedure : loop, switch : interface


L3OOP 19

Data Abstraction
Focus on the meaning of the operations (behavior), to avoid over-specification. The representation details are confined to only a small set of procedures that create and manipulate data, and all other access is indirectly via only these procedures.
Facilitates code evolution.
CS480 (Prasad) L3OOP 20

Data Abstraction : Motivation


Client/user perspective (Representation Independence) Interested in what a program does, not how. Minimize irrelevant details for clarity.
Server/implementer perspective (Information Hiding)

Restrict users from making unwarranted assumptions about the implementation. Reserve right to change representation to improve performance, (maintaining behavior).

CS480 (Prasad)

L3OOP

21

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