SlideShare une entreprise Scribd logo
1  sur  42
Syntax – Intro and Overview

           CS331
Syntax
• Syntax defines what is grammatically valid in a programming
  language
   – Set of grammatical rules
   – E.g. in English, a sentence cannot begin with a period
   – Must be formal and exact or there will be ambiguity in a
     programming language
• We will study three levels of syntax
   – Lexical
       • Defines the rules for tokens: literals, identifiers, etc.
   – Concrete
       • Actual representation scheme down to every semicolon, i.e. every lexical
         token
   – Abstract
       • Description of a program’s information without worrying about specific
         details such as where the parentheses or semicolons go
BNF Grammar
• BNF = Backus-Naur Form to specify a grammar
   – Equivalent to a context free grammar
• Set of rewriting rules (a rule that can be applied multiple
  times) defined on a set of nonterminal symbols, a set of
  terminal symbols, and a start symbol
   – Terminals, ∑ : Basic alphabet from which programs are
     constructed. E.g., letters, digits, or keywords such as “int”,
     “main”, “{“, “}”
   – Nonterminals, N : Identify grammatical categories
   – Start Symbol: One of the nonterminals which identifies the
     principal category. E.g., “Sentence” for english, “Program” for a
     programming language
Rewriting Rules
• Rewriting Rules, ρ
   – Written using the symbols  and |
     | is a separator for alternative definitions, i.e. “OR”
      is used to define a rule, i.e. “IS”

   – Format
       • LHS  RHS1 | RHS2 | RHS3 | …
       • LHS is a single nonterminal
       • RHS is any sequence of terminals and nonterminals
Sample Grammars
• Grammar for subset of English
      Sentence  Noun Verb
      Noun  Jack | Jill
      Verb  eats | bites
• Grammar for a digit
      Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 |7 |8 |9
• Grammar for signed integers
      SignedInteger  Sign Integer
      Sign  + | -
      Integer  Digit | Digit Integer
• Grammar for subset of Java
      Assignment  Variable = Expression
      Expression  Variable | Variable + Variable | Variable – Variable
      Variable  X | Y
Derivation
 • Process of parsing data using a grammar
     – Apply rewrite rules to non-terminals on the RHS of an existing
       rule
     – To match, the derivation must terminate and be composed of
       terminals only
 • Example
       Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 |7 |8 |9
       Integer  Digit | Digit Integer
     – Is 352 an Integer?
         Integer → Digit Integer → 3 Integer →
         3 Digit Integer → 3 5 Integer →
         3 5 Digit → 3 5 2

Intermediate formats are called sentential forms
This was called a Leftmost Derivation since we replaced the
leftmost nonterminal symbol each time (could also do Rightmost)
Derivation and Parse Trees
• The derivation can be
  visualized as a parse
  tree
                  Integer

          Digit             Integer

                  Digit               Integer
      3

             5                Digit

                      2
Parse Tree Sketch for Programs
BNF and Languages
• The language defined by a BNF grammar is the set of all
  strings that can be derived
   – Language can be infinite, e.g. case of integers
• A language is ambiguous if it permits a string to be parsed
  into two separate parse trees
   – Generally want to avoid ambiguous grammars
   – Example:
       • Expr  Integer | Expr + Expr | Expr * Expr | Expr - Expr
       • Parse: 3*4+1
            – Expr * Expr → Integer * Expr →
              3 * Expr → 3 * Expr+Expr → … 3 * 4 + 1
            – Expr + Expr → Expr + Integer → Expr + 1
              Expr * Expr +1 → … 3 * 4 + 1
Ambiguity
• Example for
    AmbExp  Integer | AmbExp – AmbExp
    2-3-4
Ambiguous IF Statement
Dangling ELSE:

       if (x<0)
       if (y<0) { y=y-1 }
       else { y=0 };

Does the else go with the first or second if?
Dangling Else Ambiguity
How to fix ambiguity?
• Use explicit grammar without ambiguity
  – E.g., add an “ENDIF” for every “IF”
  – Java makes a separate category for if-else vs. if:
     IfThenStatement  If (Expr) Statement
     IfThenElseStatement  If (Expr) StatementNoShortIf else
       Statement
     StatementNoShortIf contains everything except
       IfThenStatement, so the else always goes with the
       IfThenElse statement not the IfThenStatement
• Use precedence on symbols
Alternative to BNF
• The use of regular expressions is an alternate
  way to express a language
Regex to EBNF
• The book uses some deviations from “standard”
  regular expressions in Extended Backus Naur
  Format (defined in a few slides)
    { M } means zero or more occurrences of M
    ( M | N) means one of M or N must be chosen
    [M]      means M is optional

  Use “{“ to mean the literal { not the regex {
RegEx Examples
• Booleans
   – “true” | “false”
• Integers
   – (0-9)+
• Identifiers
   – (a-zA-Z){a-zA-Z0-9}
• Comments (letters/space only)
   – “//”{a-zA-Z }(“r” | “n” | “rn”)
• Regular expressions seem pretty powerful
   – Can you write one for the language anbn? (i.e. n a’s followed by n
     b’s)
Extended BNF
• EBNF – variation of BNF that simplifies
  specification of recursive rules using regular
  expressions in the RHS of the rule
• Example:
   – BNF rule
        Expr  Term | Expr + Term | Expr – Term
        Term  Factor | Term * Factor | Term / Factor
   – EBNF equivalent
        Expr  Term { [+|-] Term }
        Term  Factor { [* | / ] Factor }
• EBNF tends to be shorter and easier to read
EBNF
• Consider:
Expr  Term{ (+|-) Term }
Term  Factor { (* | / ) Factor }
Factor  Identifier | Literal | (Expr)

Parse for X+2*Y
BNF and Lexical Analysis
• Lexicon of a programming language – set of all
  nonterminals from which programs are written
• Nonterminals – referred to as tokens
   – Each token is described by its type (e.g. identifier,
     expression) and its value (the string it represents)
   – Skipping whitespace or comments


                                              or punctuation
Categories of Lexical Tokens
• Identifiers
• Literals
   Includes Integers, true, false, floats, chars
• Keywords
   bool char else false float if int main true while
• Operators
   = || && == != < <= > >= + - * / % ! [ ]
• Punctuation
   ;.{}()

   Issues to consider: Ignoring comments, role of whitespace,
      distinguising the < operator from <=, distinguishing
      identifiers from keywords like “if”
A Simple Lexical Syntax for a Small
             Language, Clite
Primary → Identifier [ "["Expression"]" ] | Literal | "("Expression")"
       | Type "("Expression")"

Identifier → Letter { Letter | Digit }
Letter → a | b | … | z | A | B | … Z
Digit → 0 | 1 | 2 | … | 9
Literal → Integer | Boolean | Float | Char
Integer → Digit { Digit }
Boolean → true | false
Float → Integer . Integer
Char → ‘ ASCIICHAR ‘
Major Stages in Compilation
• Lexical Analysis
   – Translates source into a stream of Tokens, everything else
     discarded
• Syntactic Analysis
   – Parses tokens, detects syntax errors, develops abstract
     representation
• Semantic Analysis
   – Analyze the parse for semantic consistency, transform into a
     format the architecture can efficiently run on
• Code Generation
   – Use results of abstract representation as a basis for generating
     executable machine code
Lexical Analysis & Compiling
               Process




Difficulties: 1 to many mapping from HL source to machine code
       Translation must be correct
       Translation should be efficient
Lexical Analysis of Clite
• Lexical Analysis – transforms a program
  into tokens (type, value). The rest is tossed.
• Example Clite program:
           // Simple Program
           int main() {
              int x;
              x = 3;
           }

Result of Lexical Analysis:
Lexical Analysis (2)
Result of Lexical Analysis:
 1 Type: Int Value:    int
 2 Type: Main Value:   main
 3 Type: LeftParen     Value: (   // Simple Program
 4 Type: RightParen    Value: )   int main() {
 5 Type: LeftBrace     Value: {      int x;
 6 Type: Int Value:    int           x = 3;
 7 Type: Identifier    Value: x   }
 8 Type: Semicolon     Value: ;
 9 Type: Identifier    Value: x
 10 Type: Assign       Value: =
 11 Type: IntLiteral   Value: 3
 12 Type: Semicolon    Value: ;
 13 Type: RightBrace   Value: }
 14 Type: Eof Value:   <<EOF>>
Lexical Analysis of Clite in Java
public class TokenTester {
     public static void main (String[] args) {
      Lexer lex = new Lexer (args[0]);
      Token t;
      int i = 1;

     do
     {
        t = lex.next();
        System.out.println(i+" Type: "+t.type()
                   +"tValue: "+t.value());
        i++;
      } while (t != Token.eofTok);
     }
 }

The source code for how the Lexer and Token classes are arranged
is the topic of chapter 3
Lexical to Concrete
• From the stream of tokens generated by our
  lexical analyzer we can now parse them
  using a concrete syntax
Concrete EBNF Syntax for Clite
Program → int main ( ) { Declarations Statements }
Declarations → { Declaration }
Declaration → Type Identifier [ "["Integer"]" ] { , Identifier ["["Integer"]"] };
Type → int | bool | float | char
Statements →{ Statement }
Statement → ; | Block | Assignment | IfStatement | WhileStatement
Block → { Statements }
Assignment → Identifier ["["Expression"]" ] = Expression ;
IfStatement → if "(" Expression ")" Statement [ else Statement ]
WhileStatement → while "("Expression")" Statement




                                                            Concrete Syntax;
                                                            Higher than lexical
                                                            syntax!
Concrete EBNF Syntax for Clite
Expression → Conjunction { || Conjunction }
Conjunction →Equality { && Equality }
Equality → Relation [ EquOp Relation ]
EquOp → == | !=
Relation → Addition [ RelOp Addition ]
RelOp → < | <= | > | >=                                  References lexical
Addition → Term { AddOp Term }                           syntax
AddOp → + | -
Term → Factor { MulOp Factor }
MulOp → * | / | %
Factor → [ UnaryOp ] Primary
UnaryOp → - | !
Primary → Identifier [ "["Expression"]" ] | Literal | "("Expression")" |
         Type "(" Expression ")"
Syntax Diagram
• Alternate way to specify a language
• Popularized with Pascal
• Not any more powerful than BNF, EBNF, or regular
  expressions
Linking Syntax and Semantics
• What we’ve described so far has been
  concrete syntax
  – Defines all parts of the language above the
    lexical level
     • Assignments, loops, functions, definitions, etc.
     • Uses BNF or variant to describe the language
• An abstract syntax links the concrete syntax
  to the semantic level
Abstract Syntax
• Defines essential syntactic elements without
  describing how they are concretely
  constructed
• Consider the following Pascal and C loops
   Pascal                            C
   while i<n do begin                while (i<n) {
     i:=i+1                                i=i+1;
   end                               }

Small differences in concrete syntax; identical abstract construct
Abstract Syntax Format
• Defined using rules of the form
  – LHS = RHS
     • LHS is the name of an abstract syntactic class
     • RHS is a list of essential components that define the
       class
        – Similar to defining a variable. Data type or abstract
          syntactic class, and name
        – Components are separated by ;
• Recursion naturally occurs among the
  definitions as with BNF
Abstract Syntax Example
• Loop
    Loop = Expression test ; Statement body
   – The abstract class Loop has two components, a test which is a
    member of the abstract class Expression, and a body which is a
    member of an abstract class Statement

• Nice by-product: If parsing abstract syntax in Java, it
  makes sense to actually define a class for each abstract
  syntactic class, e.g.
      class Loop extends Statement {
        Expression test;
        Statement body;
      }
Abstract Syntax of Clite
Program = Declarations decpart; Statements body;
Declarations = Declaration*
Declaration = VariableDecl   |   ArrayDecl
VariableDecl = Variable v; Type t
ArrayDecl = Variable v; Type t; Integer size
Type = int | bool | float | char
Statements = Statement*
Statement = Skip | Block | Assignment |
            Conditional | Loop
Skip =
Block = Statements
Conditional = Expression test;
             Statement thenbranch, elsebranch
Loop = Expression test; Statement body
Assignment = VariableRef target;   Expression source
Expression = VariableRef | Value | Binary | Unary
Abstract Syntax of Clite
VariableRef = Variable | ArrayRef
Binary = Operator op; Expression term1, term2
Unary = UnaryOp op; Expression term
Operator = BooleanOp | RelationalOp | ArithmeticOp
BooleanOp = && | ||
RelationalOp = = | ! | != | < | <= | > | >=
ArithmeticOp = + | - | * | /
UnaryOp = ! | -
Variable = String id
ArrayRef = String id; Expression index
Value = IntValue | BoolValue | FloatValue | CharValue
IntValue = Integer intValue
FloatValue = Float floatValue
BoolValue = Boolean boolValue
CharValue = Character charValue
Java AbstractSyntax for Clite
class Loop extends Statement {
  Expression test;
  Statement body;
}
Class Assignment extends Statement {
  // Assignment = Variable target; Expression source
  Variable target;
  Expression source;
}


…
Much more… see the file (when available)
Abstract Syntax Tree
• Just as we can build a parse tree from a BNF grammar, we
  can build an abstract syntax tree from an abstract syntax
• Example for: x+2*y
  Expression = Variable | Value | Binary
  Binary = Operator op ; Expression term1, term2




                                                   Binary node




              Expr
Sample Clite Program
• Compute nth fib number
Abstract Syntax for Loop of Clite Program
Concrete and Abstract Syntax
• Aren’t the two redundant?
   – A little bit
• The concrete syntax tells the programmer exactly
  what to write to have a valid program
• The abstract syntax allows valid programs in two
  different languages to share common abstract
  representations
   – It is closer to semantics
   – We need both!
What’s coming up?
• Semantic analysis
   – Do the types match? What does this mean?
      char a=‘c’;
      int sum=0;
      sum = sum = a;
• Can associate machine code with the abstract
  parse
   – Code generation
   – Code optimization

Contenu connexe

Tendances

Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagramMayank Jain
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysisBilalzafar22
 
Introduction to theory of computation
Introduction to theory of computationIntroduction to theory of computation
Introduction to theory of computationVinod Tyagi
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in javaHrithikShinde
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryTsegazeab Asgedom
 

Tendances (20)

Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagram
 
Java History
Java HistoryJava History
Java History
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Parsing
ParsingParsing
Parsing
 
Parsing
ParsingParsing
Parsing
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Java program structure
Java program structureJava program structure
Java program structure
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
Introduction to theory of computation
Introduction to theory of computationIntroduction to theory of computation
Introduction to theory of computation
 
Semantic analysis
Semantic analysisSemantic analysis
Semantic analysis
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Bnf and ambiquity
Bnf and ambiquityBnf and ambiquity
Bnf and ambiquity
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 

En vedette

A Brief History of Programming
A Brief History of ProgrammingA Brief History of Programming
A Brief History of Programmingjxyz
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 
Use case diagrams
Use case diagramsUse case diagrams
Use case diagramsMir Majid
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)James Titcumb
 
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)Tom Lee
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationEelco Visser
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languagesRohit Shrivastava
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsMichael Pirnat
 

En vedette (20)

The History of Programming Languages
The History of Programming LanguagesThe History of Programming Languages
The History of Programming Languages
 
A Brief History of Programming
A Brief History of ProgrammingA Brief History of Programming
A Brief History of Programming
 
History of programming
History of programmingHistory of programming
History of programming
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 
2 1 bussistem
2 1 bussistem2 1 bussistem
2 1 bussistem
 
Use case diagrams
Use case diagramsUse case diagrams
Use case diagrams
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
 
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
 
Applying SOS to MDE
Applying SOS to MDEApplying SOS to MDE
Applying SOS to MDE
 
Ch1
Ch1Ch1
Ch1
 
Finite automata
Finite automataFinite automata
Finite automata
 
Ch5b
Ch5bCh5b
Ch5b
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Ch2
Ch2Ch2
Ch2
 
01. introduction
01. introduction01. introduction
01. introduction
 
Programming Language
Programming LanguageProgramming Language
Programming Language
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 

Similaire à Syntax

Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler DesignKuppusamy P
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptxObsa2
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnfTaha Shakeel
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
CS-4337_03_Chapter3- syntax and semantics.pdf
CS-4337_03_Chapter3- syntax and semantics.pdfCS-4337_03_Chapter3- syntax and semantics.pdf
CS-4337_03_Chapter3- syntax and semantics.pdfFutureKids1
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.pptAmrita Sharma
 
Lec4slides
Lec4slidesLec4slides
Lec4slidesshawiz
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxTameneTamire
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUSkills Matter
 

Similaire à Syntax (20)

Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Control structure
Control structureControl structure
Control structure
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
Lexical1
Lexical1Lexical1
Lexical1
 
Lecture3 lexical analysis
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysis
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
CS-4337_03_Chapter3- syntax and semantics.pdf
CS-4337_03_Chapter3- syntax and semantics.pdfCS-4337_03_Chapter3- syntax and semantics.pdf
CS-4337_03_Chapter3- syntax and semantics.pdf
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.ppt
 
Lec4slides
Lec4slidesLec4slides
Lec4slides
 
Lexical
LexicalLexical
Lexical
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptx
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPU
 

Dernier

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Dernier (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Syntax

  • 1. Syntax – Intro and Overview CS331
  • 2. Syntax • Syntax defines what is grammatically valid in a programming language – Set of grammatical rules – E.g. in English, a sentence cannot begin with a period – Must be formal and exact or there will be ambiguity in a programming language • We will study three levels of syntax – Lexical • Defines the rules for tokens: literals, identifiers, etc. – Concrete • Actual representation scheme down to every semicolon, i.e. every lexical token – Abstract • Description of a program’s information without worrying about specific details such as where the parentheses or semicolons go
  • 3. BNF Grammar • BNF = Backus-Naur Form to specify a grammar – Equivalent to a context free grammar • Set of rewriting rules (a rule that can be applied multiple times) defined on a set of nonterminal symbols, a set of terminal symbols, and a start symbol – Terminals, ∑ : Basic alphabet from which programs are constructed. E.g., letters, digits, or keywords such as “int”, “main”, “{“, “}” – Nonterminals, N : Identify grammatical categories – Start Symbol: One of the nonterminals which identifies the principal category. E.g., “Sentence” for english, “Program” for a programming language
  • 4. Rewriting Rules • Rewriting Rules, ρ – Written using the symbols  and | | is a separator for alternative definitions, i.e. “OR”  is used to define a rule, i.e. “IS” – Format • LHS  RHS1 | RHS2 | RHS3 | … • LHS is a single nonterminal • RHS is any sequence of terminals and nonterminals
  • 5. Sample Grammars • Grammar for subset of English Sentence  Noun Verb Noun  Jack | Jill Verb  eats | bites • Grammar for a digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 |7 |8 |9 • Grammar for signed integers SignedInteger  Sign Integer Sign  + | - Integer  Digit | Digit Integer • Grammar for subset of Java Assignment  Variable = Expression Expression  Variable | Variable + Variable | Variable – Variable Variable  X | Y
  • 6. Derivation • Process of parsing data using a grammar – Apply rewrite rules to non-terminals on the RHS of an existing rule – To match, the derivation must terminate and be composed of terminals only • Example Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 |7 |8 |9 Integer  Digit | Digit Integer – Is 352 an Integer? Integer → Digit Integer → 3 Integer → 3 Digit Integer → 3 5 Integer → 3 5 Digit → 3 5 2 Intermediate formats are called sentential forms This was called a Leftmost Derivation since we replaced the leftmost nonterminal symbol each time (could also do Rightmost)
  • 7. Derivation and Parse Trees • The derivation can be visualized as a parse tree Integer Digit Integer Digit Integer 3 5 Digit 2
  • 8. Parse Tree Sketch for Programs
  • 9. BNF and Languages • The language defined by a BNF grammar is the set of all strings that can be derived – Language can be infinite, e.g. case of integers • A language is ambiguous if it permits a string to be parsed into two separate parse trees – Generally want to avoid ambiguous grammars – Example: • Expr  Integer | Expr + Expr | Expr * Expr | Expr - Expr • Parse: 3*4+1 – Expr * Expr → Integer * Expr → 3 * Expr → 3 * Expr+Expr → … 3 * 4 + 1 – Expr + Expr → Expr + Integer → Expr + 1 Expr * Expr +1 → … 3 * 4 + 1
  • 10. Ambiguity • Example for AmbExp  Integer | AmbExp – AmbExp 2-3-4
  • 11. Ambiguous IF Statement Dangling ELSE: if (x<0) if (y<0) { y=y-1 } else { y=0 }; Does the else go with the first or second if?
  • 13. How to fix ambiguity? • Use explicit grammar without ambiguity – E.g., add an “ENDIF” for every “IF” – Java makes a separate category for if-else vs. if: IfThenStatement  If (Expr) Statement IfThenElseStatement  If (Expr) StatementNoShortIf else Statement StatementNoShortIf contains everything except IfThenStatement, so the else always goes with the IfThenElse statement not the IfThenStatement • Use precedence on symbols
  • 14. Alternative to BNF • The use of regular expressions is an alternate way to express a language
  • 15. Regex to EBNF • The book uses some deviations from “standard” regular expressions in Extended Backus Naur Format (defined in a few slides) { M } means zero or more occurrences of M ( M | N) means one of M or N must be chosen [M] means M is optional Use “{“ to mean the literal { not the regex {
  • 16. RegEx Examples • Booleans – “true” | “false” • Integers – (0-9)+ • Identifiers – (a-zA-Z){a-zA-Z0-9} • Comments (letters/space only) – “//”{a-zA-Z }(“r” | “n” | “rn”) • Regular expressions seem pretty powerful – Can you write one for the language anbn? (i.e. n a’s followed by n b’s)
  • 17. Extended BNF • EBNF – variation of BNF that simplifies specification of recursive rules using regular expressions in the RHS of the rule • Example: – BNF rule Expr  Term | Expr + Term | Expr – Term Term  Factor | Term * Factor | Term / Factor – EBNF equivalent Expr  Term { [+|-] Term } Term  Factor { [* | / ] Factor } • EBNF tends to be shorter and easier to read
  • 18. EBNF • Consider: Expr  Term{ (+|-) Term } Term  Factor { (* | / ) Factor } Factor  Identifier | Literal | (Expr) Parse for X+2*Y
  • 19. BNF and Lexical Analysis • Lexicon of a programming language – set of all nonterminals from which programs are written • Nonterminals – referred to as tokens – Each token is described by its type (e.g. identifier, expression) and its value (the string it represents) – Skipping whitespace or comments or punctuation
  • 20. Categories of Lexical Tokens • Identifiers • Literals Includes Integers, true, false, floats, chars • Keywords bool char else false float if int main true while • Operators = || && == != < <= > >= + - * / % ! [ ] • Punctuation ;.{}() Issues to consider: Ignoring comments, role of whitespace, distinguising the < operator from <=, distinguishing identifiers from keywords like “if”
  • 21. A Simple Lexical Syntax for a Small Language, Clite Primary → Identifier [ "["Expression"]" ] | Literal | "("Expression")" | Type "("Expression")" Identifier → Letter { Letter | Digit } Letter → a | b | … | z | A | B | … Z Digit → 0 | 1 | 2 | … | 9 Literal → Integer | Boolean | Float | Char Integer → Digit { Digit } Boolean → true | false Float → Integer . Integer Char → ‘ ASCIICHAR ‘
  • 22. Major Stages in Compilation • Lexical Analysis – Translates source into a stream of Tokens, everything else discarded • Syntactic Analysis – Parses tokens, detects syntax errors, develops abstract representation • Semantic Analysis – Analyze the parse for semantic consistency, transform into a format the architecture can efficiently run on • Code Generation – Use results of abstract representation as a basis for generating executable machine code
  • 23. Lexical Analysis & Compiling Process Difficulties: 1 to many mapping from HL source to machine code Translation must be correct Translation should be efficient
  • 24. Lexical Analysis of Clite • Lexical Analysis – transforms a program into tokens (type, value). The rest is tossed. • Example Clite program: // Simple Program int main() { int x; x = 3; } Result of Lexical Analysis:
  • 25. Lexical Analysis (2) Result of Lexical Analysis: 1 Type: Int Value: int 2 Type: Main Value: main 3 Type: LeftParen Value: ( // Simple Program 4 Type: RightParen Value: ) int main() { 5 Type: LeftBrace Value: { int x; 6 Type: Int Value: int x = 3; 7 Type: Identifier Value: x } 8 Type: Semicolon Value: ; 9 Type: Identifier Value: x 10 Type: Assign Value: = 11 Type: IntLiteral Value: 3 12 Type: Semicolon Value: ; 13 Type: RightBrace Value: } 14 Type: Eof Value: <<EOF>>
  • 26. Lexical Analysis of Clite in Java public class TokenTester { public static void main (String[] args) { Lexer lex = new Lexer (args[0]); Token t; int i = 1; do { t = lex.next(); System.out.println(i+" Type: "+t.type() +"tValue: "+t.value()); i++; } while (t != Token.eofTok); } } The source code for how the Lexer and Token classes are arranged is the topic of chapter 3
  • 27. Lexical to Concrete • From the stream of tokens generated by our lexical analyzer we can now parse them using a concrete syntax
  • 28. Concrete EBNF Syntax for Clite Program → int main ( ) { Declarations Statements } Declarations → { Declaration } Declaration → Type Identifier [ "["Integer"]" ] { , Identifier ["["Integer"]"] }; Type → int | bool | float | char Statements →{ Statement } Statement → ; | Block | Assignment | IfStatement | WhileStatement Block → { Statements } Assignment → Identifier ["["Expression"]" ] = Expression ; IfStatement → if "(" Expression ")" Statement [ else Statement ] WhileStatement → while "("Expression")" Statement Concrete Syntax; Higher than lexical syntax!
  • 29. Concrete EBNF Syntax for Clite Expression → Conjunction { || Conjunction } Conjunction →Equality { && Equality } Equality → Relation [ EquOp Relation ] EquOp → == | != Relation → Addition [ RelOp Addition ] RelOp → < | <= | > | >= References lexical Addition → Term { AddOp Term } syntax AddOp → + | - Term → Factor { MulOp Factor } MulOp → * | / | % Factor → [ UnaryOp ] Primary UnaryOp → - | ! Primary → Identifier [ "["Expression"]" ] | Literal | "("Expression")" | Type "(" Expression ")"
  • 30. Syntax Diagram • Alternate way to specify a language • Popularized with Pascal • Not any more powerful than BNF, EBNF, or regular expressions
  • 31. Linking Syntax and Semantics • What we’ve described so far has been concrete syntax – Defines all parts of the language above the lexical level • Assignments, loops, functions, definitions, etc. • Uses BNF or variant to describe the language • An abstract syntax links the concrete syntax to the semantic level
  • 32. Abstract Syntax • Defines essential syntactic elements without describing how they are concretely constructed • Consider the following Pascal and C loops Pascal C while i<n do begin while (i<n) { i:=i+1 i=i+1; end } Small differences in concrete syntax; identical abstract construct
  • 33. Abstract Syntax Format • Defined using rules of the form – LHS = RHS • LHS is the name of an abstract syntactic class • RHS is a list of essential components that define the class – Similar to defining a variable. Data type or abstract syntactic class, and name – Components are separated by ; • Recursion naturally occurs among the definitions as with BNF
  • 34. Abstract Syntax Example • Loop Loop = Expression test ; Statement body – The abstract class Loop has two components, a test which is a member of the abstract class Expression, and a body which is a member of an abstract class Statement • Nice by-product: If parsing abstract syntax in Java, it makes sense to actually define a class for each abstract syntactic class, e.g. class Loop extends Statement { Expression test; Statement body; }
  • 35. Abstract Syntax of Clite Program = Declarations decpart; Statements body; Declarations = Declaration* Declaration = VariableDecl | ArrayDecl VariableDecl = Variable v; Type t ArrayDecl = Variable v; Type t; Integer size Type = int | bool | float | char Statements = Statement* Statement = Skip | Block | Assignment | Conditional | Loop Skip = Block = Statements Conditional = Expression test; Statement thenbranch, elsebranch Loop = Expression test; Statement body Assignment = VariableRef target; Expression source Expression = VariableRef | Value | Binary | Unary
  • 36. Abstract Syntax of Clite VariableRef = Variable | ArrayRef Binary = Operator op; Expression term1, term2 Unary = UnaryOp op; Expression term Operator = BooleanOp | RelationalOp | ArithmeticOp BooleanOp = && | || RelationalOp = = | ! | != | < | <= | > | >= ArithmeticOp = + | - | * | / UnaryOp = ! | - Variable = String id ArrayRef = String id; Expression index Value = IntValue | BoolValue | FloatValue | CharValue IntValue = Integer intValue FloatValue = Float floatValue BoolValue = Boolean boolValue CharValue = Character charValue
  • 37. Java AbstractSyntax for Clite class Loop extends Statement { Expression test; Statement body; } Class Assignment extends Statement { // Assignment = Variable target; Expression source Variable target; Expression source; } … Much more… see the file (when available)
  • 38. Abstract Syntax Tree • Just as we can build a parse tree from a BNF grammar, we can build an abstract syntax tree from an abstract syntax • Example for: x+2*y Expression = Variable | Value | Binary Binary = Operator op ; Expression term1, term2 Binary node Expr
  • 39. Sample Clite Program • Compute nth fib number
  • 40. Abstract Syntax for Loop of Clite Program
  • 41. Concrete and Abstract Syntax • Aren’t the two redundant? – A little bit • The concrete syntax tells the programmer exactly what to write to have a valid program • The abstract syntax allows valid programs in two different languages to share common abstract representations – It is closer to semantics – We need both!
  • 42. What’s coming up? • Semantic analysis – Do the types match? What does this mean? char a=‘c’; int sum=0; sum = sum = a; • Can associate machine code with the abstract parse – Code generation – Code optimization

Notes de l'éditeur

  1. RegExpr for CS A331, MM/DD/YY or MM/DD/YYYY