Time remaining:
Describe a system of precedence

label Programming
account_circle Unassigned
schedule 0 Hours
account_balance_wallet $5

Consider an unknown language with integer and string types in which 1+2*3 evaluates to 7, "1"+"2"+"3" evaluates to "123", "1"+2+3 evaluates to "123", and 1+"2*3" has a type error. Describe a system of precedence, associativity, overloading, and coercion that could account for this. In your system, what is the result of evaluating the expression "1"+2*3?

Apr 21st, 2015

Precedence of operators

We normally want * to have higher precedence than +. We do this by using an additional nonterminal to indicate the items that have been multiplied. The example below gives the four basic arithmetic operations their normal precedence unless overridden by parentheses. Redundant parentheses are permitted. Equal precedence operations are performed left to right.

  expr   → expr + term | expr - term | term
  term   → term * factor | term / factor | factor
  factor → digit | ( expr )
  digit  → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

We use | to indicate that a nonterminal has multiple possible right hand side. So

  A → B | C
is simply shorthand for
  A → B
  A → C

Apr 21st, 2015

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Apr 21st, 2015
...
Apr 21st, 2015
Sep 21st, 2017
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer