Formal Languages - 3

Formal Languages - 3

Formal Languages - 3 • Ambiguity in PLs – Problems with if-then-else constructs – Harder problems • Chomsky hierarchy for formal languages – Regular and context-free languages – Type 1: Context-sensitive languages – Type 0 languages and Turing machines Formal Languages-3, CS314 Fall 01© BGRyder 1 Dangling Else Ambiguity (Pascal) Start ::= Stmt Stmt ::= Ifstmt | Astmt Ifstmt ::= IF LogExp THEN Stmt | IF LogExp THEN Stmt ELSE Stmt Astmt ::= Id := Digit Digit ::= 0|1|2|3|4|5|6|7|8|9 LogExp::= Id = 0 Id ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z How are compound if statements parsed using this grammar?? Formal Languages-3, CS314 Fall 01© BGRyder 2 1 IF x = 0 THEN IF y = 0 THEN z := 1 ELSE w := 2; Start Parse Tree 1 Stmt Ifstmt IF LogExp THEN Stmt Ifstmt Id = 0 IF LogExp THEN Stmt ELSE Stmt x Id = 0 Astmt Astmt Id := Digit y Id := Digit z 1 w 2 Formal Languages-3, CS314 Fall 01© BGRyder 3 IF x = 0 THEN IF y = 0 THEN z := 1 ELSE w := 2; Start Stmt Parse Tree 2 Ifstmt Q: which tree is correct? IF LogExp THEN Stmt ELSE Stmt Id = 0 Ifstmt Astmt IF LogExp THEN Stmt x Id := Digit Astmt Id = 0 w 2 Id := Digit y z 1 Formal Languages-3, CS314 Fall 01© BGRyder 4 2 How Solve the Dangling Else? • Algol60: use block structure if x = 0 then begin if y = 0 then z := 1 end else w := 2 • Algol68: use statement begin/end markers if x = 0 then if y = 0 then z := 1 fi else w := 2 fi • Pascal: change the if statement grammar to disallow parse tree 2; that is, always associate an else with the closest if Formal Languages-3, CS314 Fall 01© BGRyder 5 New Pascal Grammar Start ::= Stmt Stmt ::= Stmt1 | Stmt2 Stmt1 ::= IF LogExp THEN Stmt1 ELSE Stmt1 | Astmt Stmt2 ::= IF LogExp THEN Stmt | IF LogExp THEN Stmt1 ELSE Stmt2 Astmt ::= Id := Digit Digit ::= 0|1|2|3|4|5|6|7|8|9 LogExp::= Id = 0 Id ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z Note: only if statements with IF..THEN..ELSE are allowed after the THEN clause of an IF-THEN-ELSE statement. Formal Languages-3, CS314 Fall 01© BGRyder 6 3 IF x = 0 THEN IF y = 0 THEN z := 1 ELSE w := 2; Start In the new grammar Stmt there is only 1 parse tree! Stmt2 IF LogExp THEN Stmt Stmt1 Id = 0 IF LogExp THEN Stmt1 ELSE Stmt1 x Id = 0 Astmt Astmt Id := Digit y Id := Digit z 1 w 2 Formal Languages-3, CS314 Fall 01© BGRyder 7 Ambiguity • Sometimes we can remove an ambiguity from a grammar by restructuring the productions, but it is not always possible – An inherently ambiguous language does not possess an unambiguous grammar – E.g., L = { ai bj ck | i = j or j = k for i,j,k >=1} generated by grammar: S ::= L C | A D L ::= a L b | ab C::= c | cC D::= bDc | bc A::= a | aA Formal Languages-3, CS314 Fall 01© BGRyder 8 4 Parse Trees S S L C A D L c C a A b D c L c C a A b D c c a b c a a a b b b parse trees for a3 b3 c3 in L problem is L contains a non-context-free language {an bn cn ,n >= 1} Formal Languages-3, CS314 Fall 01© BGRyder 9 Ambiguity • There is no algorithm which can examine an arbitrary context-free grammar and tell if it is ambiguous or not – This is undecidable • There is no algorithm which can examine two arbitrary context-free grammars and tell if they generate the same language – This is undecidable Formal Languages-3, CS314 Fall 01© BGRyder 10 5 Chomsky Hierarchy • Describes categories of languages which correspond to more and more powerful recognizing automata • 4 level hierarchy – We’ve studied bottom two levels: regular and context-free languages Formal Languages-3, CS314 Fall 01© BGRyder 11 Type 3 (regular) Languages • Recognizer: finite state automaton • Can do simple recursive constructs • Can’t count (or match parentheses) – Not regular {anbn , n>=1} • Can be written with all right recursive or all left recursive rules – Nonterm ::= term | Nonterm term Formal Languages-3, CS314 Fall 01© BGRyder 12 6 Type 2 (context-free) Languages • Recognizer: push down automaton • BNF rules with 1 nonterminal on lefthandside • Can’t check context – Not context-free {an bn cn , n>=1} – Programming examples • Check that no variable is declared twice • Check difference between function calls and array accesses in Fortran (both use parentheses) DIMENSION …. F(10,10)….F(I,J)…. Formal Languages-3, CS314 Fall 01© BGRyder 13 Context-free Languages • Check matchup of arguments with parameters in Pascal using nested function definitions procedure p (x :integer, y:real) procedure q (w: integer) … P(50,1.2)… end q …Q(1)… end P pattern seen is (parms p)(parms q) (args p) (args q) corresponding language is {an bm cn dm , m,n >= 1} Formal Languages-3, CS314 Fall 01© BGRyder 14 7 Type 1 (context-sensitive) Language • Recognizer: linear bounded automaton • Grammar rules can have more than 1 symbol on lefthandside as long as |rhs| >= |lhs| • Can do parameter - argument matching (in number) • Examples: {an bm cn dm , m,n >= 1} n n n {a b c , n >= 1} Formal Languages-3, CS314 Fall 01© BGRyder 15 Context-sensitive Example 1. T ::= S 2a 2b 2. S ::= a S B C | a B C 3. CB ::= BC --reverse B’s and C’s 4. aB ::= ab 5. bB ::= bb --expand B 6. bC ::= bc --expand C 7. cC ::= cc Derive aabbcc: 1 2a 2b 3 4 5 T ® S ® a S B C ® aaB C B C ® aa B B C C ® aabB C C ® 6 7 aabbCC ® aabbcC ® aabbcc Formal Languages-3, CS314 Fall 01© BGRyder 16 8 Type 0 (recursively- enumerable) Languages • Recognizer: Turing machine • All languages that can be recognized by a procedure • Subclass of Type 0: Recursive languages, languages recognized by an algorithm that always halts Formal Languages-3, CS314 Fall 01© BGRyder 17 Turing Machines, Lightly • Abstract model of computation • <finite set of states, alphabet, blank symbol, start state, final state, transition function> – transition function: <state, tape symbol read> ® <state, tape symbol wrote, {L,R,S}> where L,R,S means tape moves 1 square to the Left, Right, No move • TM Halting problem: Given a TM in an arbitrary configuration with nonblank symbols on its tape, will the TM eventually halt? -- unsolvable! – There cannot exist an algorithm to solve this problem for an arbitrary choice of Turing machine on arbitrary input, although for a specific TM with specific input, there may be a solution. Formal Languages-3, CS314 Fall 01© BGRyder 18 9.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us