Syntax analysis
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Syntax analysis

on

  • 5,601 views

Syntax Analysis

Syntax Analysis

Statistics

Views

Total Views
5,601
Views on SlideShare
5,601
Embed Views
0

Actions

Likes
1
Downloads
111
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Syntax analysis Presentation Transcript

  • 1. Structure of Programming Languages SYNTAX ANALYSIS VSRivera
  • 2. Syntax
    • “ The arrangement of words as elements in a sentence to show their relationship” describes the sequence of symbols that make up valid programs.
    • Form of expressions, statements and program units.
  • 3. The General Problem of Describing Syntax: Terminology
    • A sentence is a string of characters over some alphabet
    • A language is a set of sentences
    • A lexeme is the lowest level syntactic unit of a language (e.g ., * , sum, begin )
    • A token is a category of lexemes (e.g., identifier)
  • 4. Syntactic elements of the Language
    • Character set – ASCII, Unicode
    • Identifiers –restrictions on length reduces readability
    • Operator symbols - + and – represents two basic arithmetic operations.
  • 5. Syntactic elements of the Language
    • Keywords and reserved words – is an identifier used as a fixed part of the syntax of a statement. It is a reserved word if it may not be used as a programmer-chosen identifier.
    • Noise words – optional words that are inserted in a statements to improved readability.
  • 6. Syntactic elements of the Language
    • Comments – important part of the documentation. REM, /* */, or //
    • Blank (spaces)
    • Delimiters – a syntactic element used simply to mark the beginning or end of some syntactic unit such as a statement or expression. “begin”…”end”, or { }.
  • 7. Syntactic elements of the Language
    • Expressions – functions that access data objects in a program and return some value.
    • Statements
  • 8. Syntactic Analysis (parsing)
    • 2 nd stage in translation
    • Determines if the program being compiled is a valid sentence in the syntactic model programming language.
  • 9. Role of the Parser
    • Where lexical analysis splits the input into tokens, the purpose of syntax analysis (also known as parsing) is to recombine these tokens to reflect the data structure of the text.
    • The parse must also reject invalid texts by reporting syntax errors, and recover from commonly occurring errors so that it can continue processing the remainder of its input.
  • 10. Role of the Parser Lexical Analyzer Source program Get next token token Parser Rest of front end Parser Parse tree Intermediate representation
  • 11. Formal Methods of Describing Syntax
    • Grammars
    • Parse Trees
    • Syntax Diagrams
  • 12. Grammars
    • Formal definition of the syntax of a programming language.
    • Collection of rules that define, mathematically, which strings of symbols are valid sentences.
  • 13. Parts of Grammar
    • Set of tokens/terminal symbols
      • symbols that are atomic / non-divisible
      • can be combined to form valid constructs in the language
    • Set of non-terminal symbols
      • symbols used to represent intermediate definitions within the language
      • defined by productions
      • syntactic classes or categories
  • 14. Parts of Grammar
    • Set of rules called productions
      • a definition of a non-terminal symbol
      • has the form
      • x ::= y
      • where x is a non-terminal symbol and y is a sequence of symbols (non-terminal or terminal)
  • 15. Parts of Grammar
      • LHS: abstraction being defined
      • RHS: tokens, lexemes, references to other abstractions
    • Goal symbol
      • one of the set of non-terminal symbols
      • also referred to as the start symbol
  • 16. Rules to form Grammar
    • Every non-terminal symbol must appear to the left of the ::= at least one production
    • The goal symbol must not appear to the right of the ::= of any production
    • A rule is recursive if its LHS appears in its RHS
  • 17. Context Free Grammar (CFG)
    • Backus-Naur Form (BNF) Grammar
      • originally presented by John Backus (to describe ALGOL 58)and later modified by Peter Naur
    • Composed of finite set of grammar rules which define a programming language.
  • 18. Examples
    • <conditional stmt> ::=
    • if <boolean expr> then
    • <stmt>
    • else
    • <stmt>
    • | if <boolean expr> then
    • <stmt>
  • 19. Examples
    • <unsigned int> ::=
    • <digit> | <unsigned int> <digit>
    • A rule is recursive if its LHS appears in its RHS
  • 20. Examples
    • <assign> ::= <id> := <expr>
    • <id> ::= A | B | C
    • <expr> ::= <id> + <expr>
    • | <id> * <expr>
    • | ( <expr> )
    • | <id>
  • 21. Examples
    • <program> ::= begin
    • <stmt_list>
    • end
    • <stmt_list> ::=<stmt> | <stmt> <stmt_list>
    • <stmt> ::= <var> := <expression>
    • <var> ::= A | B | C
    • <expression> ::= <var> + <var>
  • 22. Grammar Derivation
    • BNF is a generative device for defining language.
    • The sentences of the language are generated through a sequence of applications of the rules, beginning with a special non-terminal (start symbol) of the grammar.
  • 23. Example
    • <program> ::= begin
    • <stmt_list>
    • end
    • begin <stmt> end
    • begin <var> := <expression> end
    • begin <var> := <var> + <var> end
    • begin A := B + C end
  • 24. Example
    • A := B * ( A + C)
    • <assign> ::= <id> := <expr>
    • := A := <expr>
    • := A := <id> * <expr>
    • := A := B * <expr>
    • := A := B * (<expr>)
    • := A := B * ( A + <expr>)
    • := A := B * ( A + <id>)
    • := A := B * ( A + C)
  • 25. When does derivation stop?
    • By exhaustingly choosing all combinations of choices, the entire language can generate.
  • 26. Exercise
    • BNF of signed integer?
    • begin A := B + C; B := C; end
  • 27. Extended BNF (EBNF)
    • Enhance the descriptive power of BNF
    • Increases the readability and writability of BNF
  • 28. Extended BNF (EBNF)
    • Notational Extensions
      • An optional element may be indicated by enclosing the element in square brackets,
      • [ … ].
      • A choice of alternative may use the symbol | within the single rule, optionally enclosed by parenthesis ( [ , ] ) if needed.
      • An arbitrary sequence of instances of element may be indicated by enclosing the element in braces followed by an asterisk, { … } + .
  • 29. Example
    • BNF
      • <expr> ::= <expr> + <term>
      • | <expr> - <term>
      • | <term>
      • <term> ::= <term> * <factor>
      • | <term> / <factor>
      • | <factor>
  • 30. Example
    • EBNF
      • <expr> ::= <term> { (+|-) <term> }
      • <term> ::= <factor> { (*|/) <factor>}
  • 31. Example
    • BNF
      • <program> ::= begin
    • <stmt_list>
    • end
  • 32. Example
    • EBNF
      • <program> ::= begin
    • <stmt> {<stmt>}
    • end
      • <program> ::= begin
    • {<stmt>} +
    • end
  • 33. Example
    • BNF
      • <signed int> ::= + <int> | - <int>
      • <int> ::= <digit> | <int> <digit>
    • EBNF
      • <signed int> ::= [+|-] <digit> {<digit>} +
  • 34. Exercise
    • EBNF of identifier?
  • 35. Solution
    • EBNF of identifier
      • <identifier> ::= <letter> {<letter> | <digit> } +
  • 36.
    • Get ½ sheet of yellow pad.
    • Prepare for a quiz.
    • Open Notes.
  • 37. Midterm Quiz #1
    • Using the following English Grammar:
    • <sentence> ::= <noun phrase> <verb phrase> .
    • <noun phrase> ::= <determiner> <noun>| <determiner> <noun> <prepositional phrase>
    • <verb phrase> ::= <verb> | <verb> <noun phrase> | <verb> <noun phrase> <prepositional phrase>
    • <prepositional phrase> ::= <preposition> <noun phrase>
    • <noun> ::= boy | girl | cat | telescope | song | feather
    • <determiner> ::= a | the
    • <verb> ::= saw | touched | surprised | sang
    • <preposition> ::= by | with
    • Write the Left Side Derivation of the sentence “ the girl touched the cat with a feather ”