Chapter 3 Describing Syntax and Semantics.ppt
《Chapter 3 Describing Syntax and Semantics.ppt》由会员分享,可在线阅读,更多相关《Chapter 3 Describing Syntax and Semantics.ppt(68页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 3 Describing Syntax and Semantics,We usually break down the problem of defining a programming language into two parts. Defining the PLs syntax Defining the PLs semanticsSyntax - the form or structure of the expressions, statements, and program unitsSemantics - the meaning of the expressions,
2、 statements, and program units.Note: There is not always a clear boundary between the two.,Introduction,Why and How,Why? We want specifications for several communities: Other language designers Implementors Programmers (the users of the language)How? One ways is via natural language descriptions (e.
3、g., users manuals, text books) but there are a number of techniques for specifying the syntax and semantics that are more formal.,Syntax Overview,Language preliminaries Context-free grammars and BNF Syntax diagrams,A sentence is a string of characters over some alphabet. A language is a set of sente
4、nces. 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). Formal approaches to describing syntax:1. Recognizers - used in compilers2. Generators - what well study,Introduction,Lexical Structure of Programming Languages
5、,The structure of its lexemes (words or tokens) token is a category of lexeme The scanning phase (lexical analyser) collects characters into tokens Parsing phase(syntactic analyser)determines syntactic structure,Stream ofcharacters,Result of parsing,tokens and values,lexical analyser,Syntactic analy
6、ser,Grammars,Context-Free Grammars Developed by Noam Chomsky in the mid-1950s. Language generators, meant to describe the syntax of natural languages. Define a class of languages called context-free languages.Backus Normal/Naur Form (1959) Invented by John Backus to describe Algol 58 and refined by
7、Peter Naur for Algol 60. BNF is equivalent to context-free grammars,A metalanguage is a language used to describe another language. In BNF, abstractions are used to represent classes of syntactic structures-they act like syntactic variables (also called nonterminal symbols), e.g.:= while do This is
8、a rule; it describes the structure of a while statement,BNF (continued),BNF,A rule has a left-hand side (LHS) which is a single non-terminal symbol and a right-hand side (RHS), one or more terminal or nonterminal symbols. A grammar is a finite nonempty set of rules A non-terminal symbol is “defined”
9、 by one or more rules. Multiple rules can be combined with the | symbol so that:= := ; And this rule are equivalent:= | ; ,Syntactic lists are described in BNF using recursion - ident| ident, A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (
10、all terminal symbols),BNF,BNF Example,Here is an example of a simple grammar for a subset of English. A sentence is noun phrase and verb phrase followed by a period.:= .:= := a | the:= man | apple | worm | penguin:= | := eats | throws | sees | is,Derivation using BNF, - .the.the man .the man .the ma
11、n eats .the man eats .the man eats the .the man eats the apple.,Another BNF Example, - - | ; - = - a | b | c | d- + | - - | const Here is a derivation:= = = = = a = = a = + = a = + = a = b + = a = b + const,Note: There is some variation in notation for BNF grammars. Here we are using - in the rules
12、instead of := .,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 may be neither leftmost nor
13、 rightmost (or something else),Derivation,Parse Tree,= a + constb,A parse tree is a hierarchical representation of a derivation,Another Parse Tree,A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees. Ambiguous grammars are, in general, very undesirable
14、 in formal languages. We can eliminate ambiguity by revising the grammar.,Grammar,Grammar,Here is a simple grammar for expressions that is ambiguous- - int- +|-|*|/The sentence 1+2*3 can lead to two different parse trees corresponding to 1+(2*3) and (1+2)*3,If we use the parse tree to indicate prece
15、dence levels of the operators, we cannot have ambiguity An unambiguous expression grammar:- - | - / const | const- / constconst const,Grammar,Grammar (continued), = - = - = const - = const - / const= const - const / const,Operator associativity can also be indicated by a grammar- + | const (ambiguou
16、s)- + const | const (unambiguous)+ const+ constconst,An Expression Grammar,Heres a grammar to define simple arithmetic expressions over variables and numbers. Exp := numExp := idExp := UnOp ExpExp := Exp BinOp ExpExp := ( Exp )UnOp := +UnOp := -BinOp := + | - | * | /A parse tree for a+b*2: _Exp_/ |
17、Exp BinOp Exp_| | / | identifier + Exp BinOp Exp| | |identifier * number,Heres another common notation variant where single quotes are used to indicate terminal symbols and unquoted symbols are taken as non-terminals.,A derivation,Heres a derivation of a+b*2 using the expression grammar: Exp = / Exp
18、 := Exp BinOp Exp Exp BinOp Exp = / Exp := id id BinOp Exp = / BinOp := + id + Exp = / Exp := Exp BinOp Exp id + Exp BinOp Exp = / Exp := num id + Exp BinOp num = / Exp := id id + id BinOp num = / BinOp := * id + id * num a + b * 2,A parse tree,A parse tree for a+b*2: _Exp_/ | Exp BinOp Exp| | / | i
19、dentifier + Exp BinOp Exp| | |identifier * number,Precedence,Precedence refers to the order in which operations are evaluated. The convention is: exponents, mult div, add sub. Deal with operations in categories: exponents, mulops, addops. Heres a revised grammar that follows these conventions:Exp :=
20、 Exp AddOp Exp Exp := Term Term := Term MulOp Term Term := Factor Factor := ( + Exp + ) Factor := num | id AddOp := + | - MulOp := * | /,Associativity,Associativity refers to the order in which 2 of the same operation should be computed 3+4+5 = (3+4)+5, left associative (all BinOps) 345 = 3(45), rig
21、ht associative if x then if x then y else y = if x then (if x then y else y), else associates with closest unmatched if (matched if has an else) Adding associativity to the BinOp expression grammarExp := Exp AddOp TermExp := Term Term := Term MulOp FactorTerm := Factor Factor := ( Exp )Factor := num
22、 | idAddOp := + | -MulOp := * | /,Another example: conditionals,Goal: to create a correct grammar for conditionals. It needs to be non-ambiguous and the precedence is else with nearest unmatched if. Statement := Conditional | whateverConditional := if test then Statement else StatementConditional :=
23、 if test then Statement The grammar is ambiguous. The 1st Conditional allows unmatched ifs to be Conditionals. if test then (if test then whatever else whatever) = correctif test then (if test then whatever) else whatever = incorrect The final unambiguous grammar. Statement := Matched | UnmatchedMat
24、ched := if test then Matched else Matched | whateverUnmatched := if test then Statement| if test then Matched else Unmatched,Syntactic sugar: doesnt extend the expressive power of the formalism, but does make it easier to use. Optional parts are placed in brackets ()- ident ( ) Put alternative parts
25、 of RHSs in parentheses and separate them with vertical bars - (+ | -) const Put repetitions (0 or more) in braces ()- letter letter | digit,Extended BNF,BNF:- + | - | - * | / | EBNF:- (+ | -) - (* | /) ,BNF,Syntax Graphs,Syntax Graphs - Put the terminals in circles or ellipses and put the nontermin
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER3DESCRIBINGSYNTAXANDSEMANTICSPPT
