lecture 5 syntax analysis
Download
Skip this Video
Download Presentation
Lecture 5: Syntax Analysis

Loading in 2 Seconds...

play fullscreen
1 / 23

Lecture 5: Syntax Analysis - PowerPoint PPT Presentation


  • 53 Views
  • Uploaded on

The University of North Carolina at Chapel Hill. Lecture 5: Syntax Analysis. COMP 144 Programming Language Concepts Spring 2002. Felix Hernandez-Campos Jan 18. Source Code. Interpre- tation. Target Code. Review: Compilation/Interpretation. Compiler or Interpreter

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 5: Syntax Analysis' - missy


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture 5 syntax analysis

The University of North Carolina at Chapel Hill

Lecture 5: Syntax Analysis

COMP 144 Programming Language Concepts

Spring 2002

Felix Hernandez-Campos

Jan 18

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

review compilation interpretation

Source Code

Interpre-

tation

Target Code

Review: Compilation/Interpretation

Compiler or Interpreter

Translation Execution

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

review syntax analysis
Review: Syntax Analysis
  • Specifying the form

of a programming

language

    • Tokens
      • Regular

Expression

    • Syntax
      • Context-Free

Grammars

Source Code

Compiler or Interpreter

Translation Execution

Target Code

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

phases of compilation
Phases of Compilation

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

syntax analysis
Syntax Analysis
  • Syntax:
    • Webster’s definition: 1 a : the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses)
  • The syntax of a programming language
    • Describes its form
      • Organization of tokens(elements)
      • Context Free Grammars (CFGs)
    • Must be recognizable by compilers and interpreters
      • Parsing
      • LL and LR parsers

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

context free grammars
Context Free Grammars
  • CFGs
    • Add recursion to regular expressions
      • Nested constructions
    • Notation

expression identifier | number| -expression

| (expression)

| expressionoperatorexpression

operator  + | -| *| /

      • Terminal symbols
      • Non-terminal symbols
      • Production rule (i.e. substitution rule)

terminal symbol  terminal and non-terminal symbols

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

parsers
Parsers
  • Scanners
    • Task: recognize language tokens
    • Implementation: Deterministic Finite Automaton
      • Transition based on the next character
  • Parsers
    • Task: recognize language syntax (organization of tokens)
    • Implementation:
      • Top-down parsing
      • Bottom-up parsing

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

parse trees
Parse Trees
  • A parse is graphical representation of a derivation
  • Example

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

parsing example
Parsing example
  • Example: comma-separated list of identifier
    • CFG

id_list  idid_list_tail

id_list_tail  ,id_list_tail

id_list_tail  ;

    • Parsing

A, B, C;

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

top down derivation of a b c

Left-to-right,

Left-most derivation

LL(1) parsing

Top-down derivation of A, B, C;

CFG

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

top down derivation of a b c1
Top-down derivation of A, B, C;

CFG

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

bottom up parsing of a b c
Bottom-up parsing of A, B, C;

CFG

Left-to-right,

Right-most derivation

LR(1) parsing

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

bottom up parsing of a b c1
Bottom-up parsing of A, B, C;

CFG

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

bottom up parsing of a b c2
Bottom-up parsing of A, B, C;

CFG

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

parsing
Parsing
  • Parsing an arbitrary Context Free Grammar
    • O(n3)
    • Too slow for large programs
  • Linear-time parsing
    • LL parsers
      • Recognize LL grammar
      • Use a top-down strategy
    • LR parsers
      • Recognize LR grammar
      • Use a bottom-up strategy

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

hierarchy of linear parsers
Hierarchy of Linear Parsers
  • Basic containment relationship
    • All CFGs can be recognized by LR parser
    • Only a subset of all the CFGs can be recognized by LL parsers

CFGsLR parsing

LL parsing

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

recursive descent parser example
Recursive Descent Parser Example
  • LL(1) grammar

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

recursive descent parser example1
Recursive Descent Parser Example
  • Outline of

recursive parser

    • This parser only

verifies syntax

    • match is

the scanner

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

recursive descent parser example2
Recursive Descent Parser Example

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

recursive descent parser example3
Recursive Descent Parser Example

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

recursive descent parser example4
Recursive Descent Parser Example

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

semantic analysis
Semantic Analysis
  • Specifying the meaning

of a programming

language

    • Attribute Grammars

Source Code

Compiler or Interpreter

Translation Execution

Target Code

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

reading assignment
Reading Assignment
  • Scott’s Chapter 2
    • Section 2.2.2
    • Section 2.2.3

COMP 144 Programming Language Concepts

Felix Hernandez-Campos

ad