0% found this document useful (0 votes)
11 views

CS420 Lecture17 SyntaxSemantics

Uploaded by

rljitler
Copyright
© © All Rights Reserved
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)
11 views

CS420 Lecture17 SyntaxSemantics

Uploaded by

rljitler
Copyright
© © All Rights Reserved
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/ 22

CS 420: Advanced

Programming Languages

Dr. Mary Pourebadi


San Diego State University
Lecture 17

Describing Syntax and


Semantics (EBNF, Static
Semantics, Attribute
Grammars, …)

(Part 3)

Book: Chapter 3

ISBN 0-321-49362-1
Extended BNF

• Optional parts are placed in brackets [ ]


<proc_call> -> ident [(<expr_list>)]

• <if_stmt> -> if (<expression>) <statement> [else


<statement>]

Without the use of brackets


• <if_stmt> -> if (<expression>) <statement>
|if (<expression>) <statement> else <statement>

Copyright © 2018 Pearson. All rights reserved. 1-3


Extended BNF

• Repetitions (0 or more) are placed inside


braces { }
• use of braces in a RHS to indicate that the enclosed part
can be repeated indefinitely or left out altogether

<ident> → letter {letter|digit}


• <ident_list> → <identifier> {,
<identifier>}

• Solve the left recursion parsing problem!

Copyright © 2018 Pearson. All rights reserved. 1-4


Extended BNF

• Alternative parts of RHSs are placed


inside parentheses ( ) and separated via
vertical bars |
<term> → <term> (+|-) const
<term> → <term> (* | / | %) <factor>

In BNF, a description of this <term> would require the following three


rules:
• <term> -> <term> * <factor>
| <term> / <factor>
| <term> % <factor>

Copyright © 2018 Pearson. All rights reserved. 1-5


BNF and EBNF
• BNF
<expr> → <expr> + <term>
| <expr> - <term>
| <term>
<term> → <term> * <factor>
| <term> / <factor>
| <factor>
<factor> → <exp> ** <factor> | <exp>
<exp> → (<expr>) | id

• EBNF
<expr> → <term> {(+ | -) <term>}
<term> → <factor> {(* | /) <factor>}
<factor> → <exp> {** <exp>}
<exp> → (<expr>) | id

Copyright © 2018 Pearson. All rights reserved. 1-6


Relation Between Generator and Recognizer

• Given a context-free grammar, a


recognizer for the language generated by
the grammar can be algorithmically
constructed

Copyright © 2018 Pearson. All rights reserved. 1-8


Static Semantics

• Nothing to do with meaning


• Context-free grammars (CFGs) cannot
describe all of the syntax of programming
languages
• Categories of constructs that are trouble:
- Context-free, but cumbersome (e.g.,
types of operands in expressions)
- Non-context-free (e.g., variables must
be declared before they are used)

Copyright © 2018 Pearson. All rights reserved. 1-9


Attribute Grammars

• Attribute grammars (AGs) have additions


to CFGs to carry some semantic info on
parse tree nodes

• Primary value of AGs:


– Static semantics specification
– Compiler design (static semantics checking)

Copyright © 2018 Pearson. All rights reserved. 1-10


Attribute Grammars : Definition

• Def: An attribute grammar is a context-free


grammar G = (S, N, T, P) with the following
additions:
– For each grammar symbol x there is a set A(x)
of attribute values
– Each rule has a set of functions that define
certain attributes of the nonterminals in the rule
– Each rule has a (possibly empty) set of
predicates to check for attribute consistency

Copyright © 2018 Pearson. All rights reserved. 1-11


Attribute Grammars: Definition

• Let X0 → X1 ... Xn be a rule

• Functions of the form S(X0) = f(A(X1), ... ,


A(Xn)) define synthesized attributes

Copyright © 2018 Pearson. All rights reserved. 1-12


Attribute Grammars: Definition

• Let X0 → X1 ... Xn be a rule

• Functions of the form S(X0) = f(A(X1), ... ,


A(Xn)) define synthesized attributes
• Functions of the form I(Xj) = f(A(X0), ... ,
A(Xn)), for i <= j <= n, define inherited
attributes

Copyright © 2018 Pearson. All rights reserved. 1-13


Attribute Grammars: Definition

• Let X0 → X1 ... Xn be a rule

• Functions of the form S(X0) = f(A(X1), ... ,


A(Xn)) define synthesized attributes
• Functions of the form I(Xj) = f(A(X0), ... ,
A(Xn)), for i <= j <= n, define inherited
attributes
• Initially, there are intrinsic attributes on the
leaves

Copyright © 2018 Pearson. All rights reserved. 1-14


Copyright © 2018 Pearson. All rights reserved. 1-15
Attribute Grammars: An Example

• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var>
and <expr>
• expected_type: inherited by <expr>

Copyright © 2018 Pearson. All rights reserved. 1-16


Attribute Grammar (continued)

• Syntax rule: <expr> → <var>[1] + <var>[2]


Semantic rules:
<expr>.actual_type  <var>[1].actual_type
Predicate:
<var>[1].actual_type == <var>[2].actual_type
<expr>.expected_type == <expr>.actual_type

Copyright © 2018 Pearson. All rights reserved. 1-17


Attribute Grammar (continued)

• Syntax rule: <var> → id


Semantic rule:
<var>.actual_type  lookup (<var>.string)

Copyright © 2018 Pearson. All rights reserved. 1-18


Attribute Grammars (continued)

• How are attribute values computed?


– If all attributes were inherited, the tree could be
decorated in top-down order.
– If all attributes were synthesized, the tree could
be decorated in bottom-up order.
– In many cases, both kinds of attributes are
used, and it is some combination of top-down
and bottom-up that must be used.

Copyright © 2018 Pearson. All rights reserved. 1-19


Attribute Grammars (continued)

<expr>.expected_type  inherited from parent

<var>[1].actual_type  lookup (A)


<var>[2].actual_type  lookup (B)
<var>[1].actual_type =? <var>[2].actual_type

<expr>.actual_type  <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type

Copyright © 2018 Pearson. All rights reserved. 1-20


Midterm Study guide

Exam Format

• Date and time: Tuesday, 10/29/2024; 3:30 pm - 4:45 pm


• => Accommodation? Email me by Tuesday 10/22

• Duration: 75 minutes

• Format: Multiple-choice questions, short-answer questions, and problem-


solving tasks

Copyright © 2018 Pearson. All rights reserved. 1-21


Midterm Study guide

Key Topics to Review

• Programming languages
• Categories and properties
• All chapters related to C programming from zyBooks
• All chapters related to C++ programming from zyBooks
• Functions and Pointers
• Syntax and semantics
• NOT INCLUDED: Lexical Analysis, Concurrency, Synchronization

Copyright © 2018 Pearson. All rights reserved. 1-22


Midterm Study guide

Resources:
• Review Lecture slides
• Review zyBooks chapters
• Suggested readings: Concepts of Programming Languages book

Practice Materials:
• Assigned PA, CA, and zyLabs from zyBooks
• Homework Assignment for syntax and semantic analysis
• Participation Discussions

Extra Notes:
• You may be asked to write pseudocode, but not actual C++ code.
• No need to memorize formulas; they will be provided if necessary.
• Be prepared to draw diagrams

Copyright © 2018 Pearson. All rights reserved. 1-23

You might also like