Docsity

Lecture Notes: ECS 140A - Programming Languages - Syntax and Semantics - Prof. Zhendong Su, Study notes of Programming Languages

Toggle sidebar
bg1
1
Prof. Su ECS 140A Lecture 2-3 1
Programming Languages
ECS 140A
13:40-15:00 Tu/Th
55 Roessler
Prof. Su ECS 140A Lecture 2-3 2
Announcements
•HW 1 has been posted on the class web page
–Written assignments on BNF/EBNF/Syntax Diagrams
–Due April 9th in class
•HW2 will also be posted later today
–Write a translator for a toy language to C
–
Will program in Java
–
Part of the code is provided
•
Office hours posted
–
Instructor: M/Th 5-6 PM
–
TA: W 3-5 PM, F 11-12
Prof. Su ECS 140A Lecture 2-3 3
Lecture Outline
•Lexical structure of a language
–Tokens: keywords, identifiers, literals, operators, etc.
–How are they specified?
•Syntax structure of a language
–Rules to combine tokens into sentences (i.e., programs)
–How are they rules specified?
•Backus-Naur Forms (BNFs)
•Extended BNFs (EBNFs)
•Syntax Diagrams (SDs)
–
How do BNF, EBNFs, and SDs relate?
•
Parser construction
–
How to build a parser from BNF/EBNF/SD specifications?
–
Recursive decent parsing
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes: ECS 140A - Programming Languages - Syntax and Semantics - Prof. Zhendong Su and more Study notes Programming Languages in PDF only on Docsity!

Prof. Su ECS 140A Lecture 2-3 1

Programming Languages

ECS 140A

13:40-15:00 Tu/Th

55 Roessler

Prof. Su ECS 140A Lecture 2-3 2

Announcements

  •  HW 1 has been posted on the class web page
    •  Written assignments on BNF/EBNF/Syntax Diagrams
    •  Due April 9th in class
  •  HW2 will also be posted later today
    •  Write a translator for a toy language to C
    •  Will program in Java
    •  Part of the code is provided
  •  Office hours posted
    •  Instructor: M/Th 5-6 PM
    •  TA: W 3-5 PM, F 11-

Prof. Su ECS 140A Lecture 2-3 3

Lecture Outline

  •  Lexical structure of a language
    •  Tokens: keywords, identifiers, literals, operators, etc.
    •  How are they specified?
  •  Syntax structure of a language
    •  Rules to combine tokens into sentences (i.e., programs)
    •  How are they rules specified?
      •  Backus-Naur Forms (BNFs)
      •  Extended BNFs (EBNFs)
      •  Syntax Diagrams (SDs)
    •  How do BNF, EBNFs, and SDs relate?
  •  Parser construction
    •  How to build a parser from BNF/EBNF/SD specifications?
    •  Recursive decent parsing

Prof. Su ECS 140A Lecture 2-3 4

What Characterizes a Language

  •  Syntax
    •  Symbols ( lexical)
      •  Keywords: if, while, for …
      •  Identifiers: variable names (x, i), function names (foo) …
      •  Literals: 100, 1.0, “foo” …
      •  Operators: +, -, *, / …
      •  Others: (, ), {, }, >, =, …
    •  Rules for combining the symbols ( syntactic)
      •  To determine the legal sentences (programs) in a language
      •  E.g.: if then else
  •  Semantics
    •  The meaning of a sentence

Prof. Su ECS 140A Lecture 2-3 5

The Structure of a Compiler

1. Lexical Analysis

2. Parsing

3. Semantic Analysis

4. Optimization

5. Code Generation

The first 3, at least, can be understood by

analogy to how humans comprehend English.

Prof. Su ECS 140A Lecture 2-3 6

Lexical Analysis

  •  First step: recognize words.
    •  Smallest unit above letters

This is a sentence.

  •  Note the
    •  Capital “T” (start of sentence symbol)
    •  Blank “ “ (word separator)
    •  Period “.” (end of sentence symbol)

Prof. Su ECS 140A Lecture 2-3 10

Diagramming a Sentence

This line is a longer sentence

article noun verb article adjective noun

subject object

sentence

Prof. Su ECS 140A Lecture 2-3 11

Parsing Programs

  •  Parsing program expressions is the same
  •  Consider:

If x == y then z = 1; else z = 2;

  •  Diagrammed:

if-then-else

x == y z 1 z 2

relation assign assign

predicate then-stmt else-stmt

Prof. Su ECS 140A Lecture 2-3 12

Semantic Analysis

  •  Once sentence structure is understood, we

can try to understand “meaning”

  •  But meaning is too hard for compilers
  •  Compilers perform limited analysis to catch

inconsistencies

Prof. Su ECS 140A Lecture 2-3 13

Semantic Analysis in English

  •  Example:

Jack said Jerry left his assignment at home.

What does “his” refer to? Jack or Jerry?

  •  Even worse:

Jack said Jack left his assignment at home?

How many Jacks are there?

Which one left the assignment?

Prof. Su ECS 140A Lecture 2-3 14

Semantic Analysis in Programming

  •  Programming

languages define

strict rules to avoid

such ambiguities

  •  This C++ code prints

“4”; the inner

definition is used

int Jack = 3;

int Jack = 4;

cout << Jack;

Prof. Su ECS 140A Lecture 2-3 15

More Semantic Analysis

  •  Compilers perform many semantic checks

besides variable bindings

  •  Example:

Jack left her homework at home.

  •  A “type mismatch” between her and Jack; we

know they are different people

  •  Presumably Jack is male

Prof. Su ECS 140A Lecture 2-3 19

Syntax: Backus-Naur Form (BNF)

  •  A meta-language for describing the syntax of a programming language
  •  First used in Algol60 report (in 1959)
  •  Many variants, but all similar and equivalent in expressive power to context-free grammars
  •  Has four parts
    •  A set of terminals (tokens)
    •  A set of non-terminals (NT)
    •  A set of production (or grammar) rules
    •  A start non-terminal (like “main” in a program)

Prof. Su ECS 140A Lecture 2-3 20

BNF (cont.)

  •  Example: ::= NUM | + | * | ()
    •  A grammar for arithmetic expressions
      •  Terminal: NUM, +, *, (, )
      •  Non-terminal:
      •  Rule: ::= NUM | + | * | ()
      •  Start non-terminal:
    •  Intuitively it is just a recursive definition:
      •  NUM is an expression
      •  The addition/multiplication of 2 expressions is also an expression
      •  Parenthesized expressions are also expressions

Prof. Su ECS 140A Lecture 2-3 21

BNF (cont.)

  •  BNF uses the following notations
    •  Non-terminals enclosed in < and >:
    •  Rules written as: X ::= RHS
      •  X must be a non-terminal
      •  RHS can be
        •  A sequence of terminals and non-terminals, or
        •  Sequences of terminals and non-terminals seperated by the symbol | (meaning “or”)
      •  If RHS is empty (i.e., length 0 sequence), we use 

Prof. Su ECS 140A Lecture 2-3 22

BNF (cont.)

  •  Convention
    •  Specify a grammar as a list of production rules
    •  List the rules for the starting NT first
  •  The meaning of a grammar G
    •  It defines a language of strings
    •  The grammar derives a string by
      •  Starting with the starting NT
      •  Repeatedly replacing a NT by the RHS of one of its rules until only terminals remain
      •  Example: -> + -> NUM + -> NUM + NUM
      •  This is called a derivation
    •  The language L(G) of a grammar G = the set of strings derivable from G’s starting NT

Prof. Su ECS 140A Lecture 2-3 23

Another Example

  •  Terminals: NUM, +, *, (, )
  •  Non-terminals: , ,
  •  Starting NT:
  •  Productions: ::= + | ::= * | ::= () | NUM
  •  Is this different from our earlier example?

::= NUM | + | * | ()

Prof. Su ECS 140A Lecture 2-3 24

Parse Tree

  •  A parser of a compiler builds parse trees
  •  What is a parse tree?
    •  Graphical representation of the syntax structure of a string
    •  Making explicit how the string is generated from the rules
      •  Root: starting NT
      •  Interior nodes: NTs
      •  Leaves: terminals
      •  Edges: node X to nodes a 1 , …, an for a rule X ::= a 1 … an
    •  Example:
      •  Consider the string 1 * (2 + 3)
      •  Show its derivation
      •  Show the corresponding parse tree

Prof. Su ECS 140A Lecture 2-3 28

Conversions between BNF and EBNF

  •  BNF => EBNF
    •  Recursion in grammar
      •  A ::= a A | B => A ::= { a } B
    •  Common string to factor out with grouping and options
      •  A ::= a B | a => A ::= a [B]
      •  A ::= a B | a C => A ::= a (B | C)
  •  EBNF => BNF
    •  Options: [ ]
      •  A ::= a [B] C => A ::= a N C N ::= B | 
    •  Repetition: { }
      •  A ::= a { B1 B2 ... Bn } C => A ::= a N C N ::= B1 B2 ... Bn N | 
    •  Grouping: ()
      •  A ::= a (B | C) D => A ::= a N D N ::= B | C

Prof. Su ECS 140A Lecture 2-3 29

Syntax Diagrams

  •  Graphical representation of grammar rules
  •  Less commonly used now
  •  Direct mapping from EBNF

(1) terminal x: x (2) non-terminal Y: Y

(3) sequence Y Z: Y Z

(4) alternation Y | Z: Y

Z

(5) optional [Y]:

Y

(6) repetition {Y}:

Y

Prof. Su ECS 140A Lecture 2-3 30

Example

•  EBNF:

Program ::= Block Block ::= { Stmt } Stmt ::= Assign | While Assign ::= id ‘:=‘ Exp Exp ::= id | num While ::= while Exp do Block end Note: id (identifiers) and num (numeric literals) are terminals

  •  Example: while id do id := 3 end
  •  Give syntax diagram

Prof. Su ECS 140A Lecture 2-3 31

Parsing

  •  What does a parser do?
    •  Recognizes strings in a language
    •  If yes, builds the parse tree for the string
  •  Many ways to do this (in ECS 142)
  •  Here, we will look at a simple method
    •  Top-down parsing: recursive decent parser
    •  Predicative parser with a single look-ahead token
    •  General approach
      •  Determine “First” sets
      •  Translate syntax together with the “First” sets into parser code

Prof. Su ECS 140A Lecture 2-3 32

First Sets

  •  First(X) = set of all terminals that can begin a string

derived from X, and  if X derives 

  •   denotes the empty string
  •  Example (grammar on slide 30)
  •  First(While) = { while }
  •  First(Exp) = { id, num }
  •  First(Assign) = { id }
  •  First(Stmt) = First(Assign) U First(While) = { id, while }
  •  First(Block) = {  } U First(Stmt) = { , id, while }
  •  First(Program) = { , id, while }

Prof. Su ECS 140A Lecture 2-3 33

Constructing the Parser

  •  Show the syntax diagram for example grammar
  •  Assume
    •  sym: global variable to store a token
    •  next: routine that sets sym to token in input
    •  f_X: denotes first(X)
    •  error: error handling routine
  •  Construct one procedure for each non-terminal
    •  procedure X() for non-terminal X