cse443 compilers
play

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei Phases of a Syntactic compiler structure Figure 1.6,


  1. CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei

  2. Phases of a Syntactic compiler structure Figure 1.6, page 5 of text

  3. Language terminology (from Sebesta (10 th ed), p. 115) • A language is a set of strings of symbols, drawn from some finite set of symbols (called the alphabet of the language). • “The strings of a language are called sentences ” • “Formal descriptions of the syntax […] do not include descriptions of the lowest-level syntactic units […] called lexemes .” • “A token of a language is a category of its lexemes.” • Syntax of a programming language is often presented in two parts: – regular grammar for token structure (e.g. structure of identifiers) – context-free grammar for sentence structure 5

  4. Examples of lexemes and tokens Lexemes Tokens foo identifier i identifier sum identifier -3 integer_literal 10 integer_literal 1 integer_literal ; statement_separator = assignment_operator 6

  5. Backus-Naur Form (BNF) • Backus-Naur Form (1959) – Invented by John Backus to describe ALGOL 58, modified by Peter Naur for ALGOL 60 – BNF is equivalent to context-free grammar – BNF is a metalanguage used to describe another language, the object language – Extended BNF: adds syntactic sugar to produce more readable descriptions 7

  6. BNF Fundamentals • Sample rules [p. 128] <assign> → <var> = <expression> <if_stmt> → if <logic_expr> then <stmt> <if_stmt> → if <logic_expr> then <stmt> else <stmt> • non-terminals/tokens surrounded by < and > • lexemes are not surrounded by < and > • keywords in language are in bold • → separates LHS from RHS • | expresses alternative expansions for LHS <if_stmt> → if <logic_expr> then <stmt> | if <logic_expr> then <stmt> else <stmt> • = is in this example a lexeme 8

  7. BNF Rules • A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal and nonterminal symbols • A grammar is often given simply as a set of rules (terminal and non-terminal sets are implicit in rules, as is start symbol) 9

  8. Describing Lists • There are many situations in which a programming language allows a list of items (e.g. parameter list, argument list). • Such a list can typically be as short as empty or consisting of one item. • Such lists are typically not bounded. • How is their structure described? 10

  9. Describing lists • The are described using recursive rules . • Here is a pair of rules describing a list of identifiers, whose minimum length is one: <ident_list> -> ident | ident , <ident_list> • Notice that ‘ , ’ is part of the object language (the language being described by the grammar). 11

  10. Derivation of sentences from a grammar • A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) 12

  11. Recall example 2 G 2 = ({a, the, dog, cat, chased}, {S, NP, VP, Det, N, V}, {S à NP VP, NP à Det N, Det à a | the, N à dog | cat, VP à V | VP NP, V à chased}, S) 13

  12. Example: derivation from G 2 • Example: derivation of the dog chased a cat S à NP VP à Det N VP à the N VP à the dog VP à the dog V NP à the dog chased NP à the dog chased Det N à the dog chased a N à the dog chased a cat 14

  13. Example 3 L 3 = { 0, 1, 00, 11, 000, 111, 0000, 1111, … } G 3 = ( {0, 1}, {S, ZeroList, OneList}, {S à ZeroList | OneList, ZeroList à 0 | 0 ZeroList, OneList à 1 | 1 OneList }, S ) 15

  14. Example: derivations from G 3 • Example: derivation of 0 0 0 0 S à ZeroList à 0 ZeroList à 0 0 ZeroList à 0 0 0 ZeroList à 0 0 0 0 • Example: derivation of 1 1 1 S à OneList à 1 OneList à 1 1 OneList à 1 1 1 16

  15. Observations about derivations • Every string of symbols in the derivation is a sentential form. • A sentence is a sentential form that has only terminal symbols. • A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded. • A derivation can be leftmost, rightmost, or neither. 17

  16. An example programming language grammar fragment <program> -> <stmt-list> <stmt-list> -> <stmt> | <stmt> ; <stmt-list> <stmt> -> <var> = <expr> <var> -> a | b | c | d <expr> -> <term> + <term> | <term> - <term> <term> -> <var> | const 18

  17. A leftmost derivation of a = b + const <program> => <stmt-list> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const 19

  18. Parse tree • A parse tree is an hierarchical representation of a derivation: <program> <stmt-list> <stmt> <var> = <expr> a <term> + <term> <var> const b 20

  19. Parse trees and compilation • A compiler builds a parse tree for a program (or for different parts of a program). • If the compiler cannot build a well-formed parse tree from a given input, it reports a compilation error. • The parse tree serves as the basis for semantic interpretation/translation of the program. 21

  20. Extended BNF • Optional parts are placed in brackets [ ] <proc_call> -> ident [(<expr_list>)] • Alternative parts of RHSs are placed inside parentheses and separated via vertical bars <term> -> <term> (+|-) const • Repetitions (0 or more) are placed inside braces { } <ident> -> letter {letter|digit} 22

  21. Comparison of BNF and EBNF • sample grammar fragment expressed in BNF <expr> -> <expr> + <term> | <expr> - <term> | <term> <term> -> <term> * <factor> | <term> / <factor> | <factor> • same grammar fragment expressed in EBNF <expr> -> <term> {(+ | -) <term>} <term> -> <factor> {(* | /) <factor>} 23

  22. Ambiguity in grammars • A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees • Operator precedence and operator associativity are two examples of ways in which a grammar can provide an unambiguous interpretation. 24

  23. Operator precedence ambiguity The following grammar is ambiguous: <expr> -> <expr> <op> <expr> | const <op> -> / | - The grammar treats the '/' and '-' operators equivalently. 25

  24. An ambiguous grammar for arithmetic expressions <expr> -> <expr> <op> <expr> | const <op> -> / | - <expr> <expr> <expr> <op> <expr> <expr> <op> <op> <expr> <expr> <op> <expr> <expr> <op> <expr> const - const / const const - const / const 26

  25. Disambiguating the grammar • If we use the parse tree to indicate precedence levels of the operators, we can remove the ambiguity. • The following rules give / a higher precedence than - <expr> -> <expr> - <term> | <term> <term> -> <term> / const | const <expr> <expr> - <term> <term> <term> / const const const 28

  26. Links to BNF-style grammars for actual programming languages Below are some links to grammars for real programming languages. Look at how the grammars are expressed. – http://www.schemers.org/Documents/Standards/R5RS/ – http://www.sics.se/isl/sicstuswww/site/documentation.html In the ones listed below, find the parts of the grammar that deal with operator precedence. – http://java.sun.com/docs/books/jls/index.html – http://www.lykkenborg.no/java/grammar/JLS3.html – http://www.enseignement.polytechnique.fr/profs/informatique/Jean- Jacques.Levy/poly/mainB/node23.html – http://www.lrz-muenchen.de/~bernhard/Pascal-EBNF.html 29

  27. <expression> <assignment-expression> Derivation of <conditional-expression> 2+5*3 <logical-OR-expression> <logical-AND-expression> using C grammar <inclusive-OR-expression> <exclusive-OR-expression> <AND-expression> <equality-expression> <relational-expression> <shift-expression> <additive-expression> + <additive-expression> <multiplicative-expression> <multiplicative-expression> <multiplicative-expression> <cast-expression> * <cast-expression> <unary-expression> <cast-expression> <unary-expression> <postfix-expression> <unary-expression> <postfix-expression> <primary-expression> <postfix-expression> <primary-expression> <constant> <primary-expression> <constant> 3 <constant> 30 2 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend