Intermezzo: Symbols (1) Intermezzo: Symbols (2) A complex symbol is: A complex symbol is: An elementary symbol is: – Repetition: – Literal: “abc” l “ b ” Li ● S* zero or more times S S ; S+ one or more times S mes: INT – Sort (non-terminal) nam ● {S1 S2} ● {S1 S2}* zero or more times S1 separated by S2 times S1 separated by S2 zero or more – Character classes: [a-z] : : one of a , b , ..., z ● {S1 S2}+ one or more t times S1 separated by S2 ● ~ : complement of charac p cter class. – Optional: S? zero or one l S? e occurrences of S f S O ti ● / : difference of two char racter classes. – Alternative: S | T an S o or a T ● /\ : intersection of two ch /\ : intersection of two ch haracter classes haracter classes. for “<” S “,” T “>” – Tuple: <S,T> shorthand f ● \/ : union of two charact er classes. – Parameterized sorts: S[[ [ P1, P2 ]] Introduction to ASF+SDF 1 Introduction to ASF+SDF 2 Intermezzo: produ uctions (functions) Example: floatin ng point numbers sorts UnsignedInt SignedInt Unsig gnedReal Number ● General form of a produ uction (function): lexical syntax – S1 S2 ... Sn -> S0 Attr 1 2 0 ributes b [0] | ([1-9][0-9]*) -> UnsignedInt ● Lexical syntax and context- -free syntax are similar, but [\+\-]? UnsignedInt [\ \ ]? UnsignedInt SignedInt -> SignedInt – Between the symbols in a production optional layout UnsignedInt "." [0-9]+ ([eE] Sig gnedInt)? -> UnsignedReal symbols may occur in th he input text. Unsi n dInt [ E] Si n dInt UnsignedInt [eE] SignedInt > Unsi n dR -> UnsignedReal l – A context-free productio on is equivalent with: UnsignedInt | UnsignedReal -> Number ● S1 LAYOUT? S2 LAY ● S1 LAYOUT? S2 LAY YOUT? ... LAYOUT? Sn YOUT? LAYOUT? Sn -> S0 S0 0 1 14 0.1 3e4 3.014e-7 00 01 04.1 3e04 3.14e-07 Introduction to ASF+SDF 3 Introduction to ASF+SDF 4
Intermezzo: disa Intermezzo: disa ambiguation (1) ambiguation (1) Intermezzo: list ts, lists, lists, ... Assume: “a” “ ” -> A A A ● Why manual disambiguation Wh l di bi ti i SDF? in SDF? – Fully declarative definition of syntax x a a a a A+ – Implicit disambiguation can be wron I li i di bi i b ng {A “;”}+ a a a ; a a ; a; a a ; a; a; – More programming languages can b e parsed – Modularity Modularity (A “;”)+ a ; a ; a ; a; a ; ; a ; a; a a ; a; a – Fewer non-terminals/sorts a ; a; – Separation of concerns: form of rule p es is independent of disambiguation p g (A “;”?)+ ( “ ) a a a a a a a ; a a ; a; ● Costs? – Intellectual effort Intellectual effort – False safety, grammar may still be am mbiguous Introduction to ASF+SDF 5 Introduction to ASF+SDF Intermezzo: disa Intermezzo: disa ambiguation (2) ambiguation (2) Intermezzo: disa Intermezzo: disa ambiguation (3) ambiguation (3) module Expressions module Expressions ● Associativity of binary op A i ti it f bi perators t exports ● Priorities between binary y operators p sorts E – Transitivity of priorities lexical syntax [\ \t\n] -> LAYOUT – Prefix expressions and bin Prefix expressions and bin nary expressions nary expressions context-free start-symbols E – Postfix expression with bi inary operator (slide 3+4) context-free syntax – Overloaded comma’s in ex O l d d ’ i xpression languages (slide 5+6) i l ( lid 5 6) "e" -> E E "[" E "]" -> E E "+" E -> E {left} Input sentence: e + e [ e + e ] I t t + [ + ] Introduction to ASF+SDF Introduction to ASF+SDF
Intermezzo: disa Intermezzo: disa ambiguation (4) ambiguation (4) Intermezzo: disa Intermezzo: disa ambiguation (5) ambiguation (5) module Expressions module Expressions module Expressions module Expressions exports exports sorts E sorts E lexical syntax lexical syntax [\ \t\n] -> LAYOUT [\ \t\n] -> LAYOUT context-free start-symbols E context-free start-symbols E context-free syntax context-free syntax "e" -> E e > E "e" -> E "f" "(" {E ","}+ ")" -> E E "," E -> E {left} context-free priorities E "[" E "]" -> E <0> > Input sentence: f(e,e) E "+" E E "+" E -> E {left} > E {l ft} Introduction to ASF+SDF Introduction to ASF+SDF Intermezzo: disa Intermezzo: disa ambiguation (6) ambiguation (6) Intermezzo: disa Intermezzo: disa ambiguation (7) ambiguation (7) module Expressions module Expressions ● Longest match L t t h exports ● Runaway whitespace and y p d comments sorts E lexical syntax ● Syntactic overloading bet tween identifiers and [\ \t\n] -> LAYOUT keywords keywords context free start symbols E context-free start-symbols E context-free syntax ● Useless cloning of identif fier classes "e" -> E "f" "(" {E ","}+ ")" -> E ● Dynamically reserved typ pes context-free priorities ● Dangling else and related ● Dangling else and related d ambiguities d ambiguities { non assoc: E > {E , }+ { non-assoc: E -> {E “,”}+ E "," E -> E {left} } Introduction to ASF+SDF Introduction to ASF+SDF
Intermezzo: disa Intermezzo: disa ambiguation (8) ambiguation (8) ● Runaway whitespace and commen R hit d nts t module Layout exports p sorts P S context-free start-symbols P lexical syntax [\ \t\n] -> LAYOUT context-free restrictions LAYOUT? -/- [\ \t\n] context-free syntax "[" S* "]" -> P "s" -> S Introduction to ASF+SDF
Recommend
More recommend