cs 208: computing theory
DESCRIPTION
CS 208: Computing Theory. Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics. Context Free Languages. Context-Free Languages. So far …. Methods for describing regular languages Finite Automata Deterministic Non-deterministic Regular Expressions - PowerPoint PPT PresentationTRANSCRIPT
-
CS 208: Computing Theory
Assoc. Prof. Dr. Brahim HnichFaculty of Computer SciencesIzmir University of Economics
-
Context Free LanguagesContext-Free Languages
-
So far Methods for describing regular languagesFinite AutomataDeterministicNon-deterministicRegular ExpressionsThey are all equivalent, and limitedCannot some simple languages like {0n1n | n is positive}Now, we introduce a more powerful method for describing languagesContext-free Grammars (CFG)
-
Are CFGs any useful?Extremely useful!
Artificial IntelligenceNatural language Processing
Programming Languagesspecificationcompilation
-
ExampleThis is a CFG which we call G1A0A1ABB#
-
Example: production rulesThis is a CFG which we call G1A0A1ABB#Each line is a substitution rules or production rules
-
Example: variablesThis is a CFG which we call G1A0A1ABB#A and B are called variables or non-terminals
-
Example: variablesThis is a CFG which we call G1A0A1ABB#0,1, and # are called terminals
-
Example: variablesThis is a CFG which we call G1A0A1ABB#A is the start variable
-
RulesWe use a CFG to describe a language by generating each string of that languageWrite down the start variablePick a variable written down and a production rule that starts with that variableReplace that variable with right-hand side of the production ruleRepeat until no variable remain
-
DerivationsThis is a CFG which we call G1A0A1ABB#Derivations with G1A0A10B10#1A0A100A1100B1100#11A0A100A11000A111000B111000#111
-
Parse treeParse tree for 0#1 in G1A0A10B10#1
AB0#1A
-
Parse treeParse tree for 00#11 in G1 A0A100A1100B1100#11AB0#1AA01
-
Context-free languages
All strings generated by a CFG constitute the language of the grammarExample: L(G1)={0n#1n | n is positive}
Any language generated by a context-free grammar is a context-free language
-
A useful abbreviationProduction rulesA 0A1A BB #Can be written asA 0A1 | BB #
-
Another exampleCFG G2 describing a fragment of English ||| a | the boy | girl | flower touches | likes | sees with
-
Another exampleExamples of strings belonging to L(G2)
a boy sees
the boy sees a flower
a girl with a flower likes the boy with a flower
-
Another exampleDerivation of a boy sees a a boy a boy a boy a boy sees
-
Formal definitionsA context-free grammar is a 4-tuple whereV is a finite set of variablesis a finite set of terminalsR is a finite set of rules: each rule is a variable and a finite string of variable and terminalsS is the start symbol
-
Formal definitionsIf u and v are strings of variable and terminals, andA w is a rule of the grammar,Then uAv yields uwv, written uAv uwv
We write u * v if u = v oru u1 . uk v
-
Formal definitionsThe language of grammar G is
L(G) = {w | S * w}
-
ExampleConsider G4 = where R isS (S) | SS | What is the language of G4?Examples: (), (()((())),
-
ExampleConsider G4 = where R isS (S) | SS | What is the language of G4?L(G4) is the set of strings of properly nested parenthesis
-
ExampleConsider G4 = where R isE E + T | TT T X F | FF (E) | a
What is the language of G4?Examples: a+a+a, (a+a) x a
-
ExampleConsider G4 = where R isE E + T | TT T x F | FF (E) | a
What is the language of G4?E stands for expression, T for Term, and F for Factor: so this grammar describes some arithmetic expressions
-
AmbiguitySometimes a grammar can generate the same string in several different ways!This string will have several parse treesThis is a very serious problemThink if a C program can have multiple interpretations?If a language has this problem, we say that it is ambiguous
-
ExampleConsider G5:+|x |() | aG5 is ambiguous because a+axa has two parse tress!
-
ExampleConsider G5:+|x |() | aG5 is ambiguous because a+axa has two parse tress!
a+ a xa
-
ExampleConsider G5:+|x |() | aG5 is ambiguous because a+axa has two parse tress!
a+ a xa
a+ a xa
-
Formal definition: ambiguityA string w is generated ambiguously in CFG G if it has two or more different leftmost derivations!A derivation is leftmost if at every step the variable being replaced is the leftmost one
Grammar G is ambiguous if it generates some string ambiguously
-
Chomsky Normal Form (CNF)Every rule has the formA BCA aS Where S is the start symbol, A, B, and C are any variables except that B and C may not be the start symbol
-
TheoremTheorem: Any context-free language is generated by a context-free grammar in Chomsky normal formHow?Add new start symbol S0Eliminate all rules of the form A Eliminate all unit rules of the form A BPatch up rules so that grammar denotes the same languageConvert remaining rules to proper form
-
Steps to convert any grammar into CNFStep1Add a new start symbol S0Add the rule S0S
-
Steps to convert any grammar into CNFStep2: RepeatRemove some rule of the form A where A is not the start symbolThen, for each occurrence of A on the right-hand side of a rule, we add a new rule with that occurrence deletedE.g., if R uAvAu where u and v are strings of variables and terminalsWe add rules: R uvAu, RuAvu, and RuvuFor RA add R, except if R has already been removedUntil all -rules not involving the start symbol have been removed
-
Steps to convert any grammar into CNFStep3: eliminate unit rulesRepeatRemove some rule of the form A BFor each Bu, add Au, except if Au has already been removedUntil all unit rules have been removed
-
Steps to convert any grammar into CNFStep4: convert remaining rulesReplace each rule A u1u2uk, where k >2 and each ui is a terminal or a variable with the rulesAu1A1A1u2A2A2u3A3.Ak-2uk-1ukIf k=2, we replace any terminal ui in the preceding rules with the new variable Ui and add the rule Uiui
-
ExampleStart withS ASA | aBA B | SB b |
-
ExampleStep 1: add new start symbol and new ruleS0 SS ASA | aBA B | SB b |
-
ExampleStep 2: remove -rule B S0 SS ASA | aB | aA B | S | B b
-
ExampleStep 2: remove -rule A S0 SS ASA | aB | a | SA | AS | SA B | S B b
-
ExampleStep 3: remove unit rule S SS0 SS ASA | aB | a | SA | AS | SA B | S B b
-
ExampleStep 3: remove unit rule S0 SS0 S | ASA | aB | a | SA | ASS ASA | aB | a | SA | AS A B | S B b
-
ExampleStep 3: remove unit rule A BS0 ASA | aB | a | SA | ASS ASA | aB | a | SA | AS A B | S | bB b
-
ExampleStep 3: remove unit rule A SS0 ASA | aB | a | SA | ASS ASA | aB | a | SA | AS A S | b | ASA | aB | a | SA | ASB b
-
ExampleStep 3: remove unit rule A SS0 ASA | aB | a | SA | ASS ASA | aB | a | SA | AS A b | ASA | aB | a | SA | ASB b
-
ExampleStep 4: convert remaining rulesS0 AA1|UB| a| SA | ASS AA1|UB | a | SA | AS A b | AA1 | UB | a | SA | ASB bUaA1SA
-
Pushdown Automata
-
Pushdown automataPushdown automat (PDA) are like nondeterministic finite automat but have an extra component called a stackCan push symbols onto the stackCan pop them (read them back) laterStack is potentially unbounded
-
State controlaabaxyzstackinput
-
Formal DefinitionA pushdown automaton is a 6-tuple (Q,,S, ,q0,F), whereQ is a finite set of states is a finite set of symbols called the alphabetS is the stack alphabet : Q x x S P(Q x S) is the transition functionq0 Q is the start stateF Q is the set of accept states or final states
-
ConventionsQuestion: when is the stack empty?Start by pushing a $ onto the stackIf you see it again, stack is empty
Question: when is input string emptyDoesnt matterAccepting states accept only if inputs exhausted
-
NotationTransition a,bc means Read a from the inputPop b from stackPush c onto stackMeaning of transitionIf a = , dont read inputIf b= , dont pop any symbolIf c= , dont push any symbols
-
ExampleRecall 0n1n which is not regularConsider the following PDARead input symbolsFor each 0, push it on the stackAs soon as a 1 is seen, pop a 0 for each 1 readAccept if stack is empty when last symbol readReject if stack non-empty, or if input symbol exist, or if 0 read after a 1, etc
-
Example,$0, 01,0 1,0 ,$ {0n1n| n is positive}
-
Example,${aibjck| i=j or i=k}a, a, , , , $ b, c, a b, a ,$ c,
-
TheoremTheorem: A language is context-free if and only some pushdown automaton accepts itProof: we will skip it! (Those interested may read the book)Corollary: Every regular language is a context-free languageRegular languagesContext-free languages
-
Conclusions
Context-free grammarsdefinitionambiguityChomsky normal form
Pushdown automatadefinition
Next: Part C; Computability Theory