Context Free Languages and Pushdown Automata

Size: px
Start display at page:

Download "Context Free Languages and Pushdown Automata"

Transcription

1 Context Free Languages and Pushdown Automata COMP2600 Formal Methods for Software Engineering Ranald Clouston Australian National University Semester 2, 2013 COMP 2600 Context Free Languages and Pushdown Automata 1

2 Parsing The process of parsing a program is partly about confirming that a given program is well-formed syntax. But it is also about representing the structure of the program so that it can be executed semantics. For this purpose, the trail of sentential forms created en route to generating a given sentence is just as important as the question of whether the sentence can be generated or not. COMP 2600 Context Free Languages and Pushdown Automata 2

3 The Semantics of Parses Take the code if e1 then if e2 then s1 else s2 where e1, e2 are boolean expressions and s1, s2 are subprograms. Does this mean if e1 then ( if e2 then s1 else s2 ) or if e1 then ( if e2 else s1 ) else s2 We d better have an unambiguous way to tell which is right, or we cannot know what the program will do at runtime! COMP 2600 Context Free Languages and Pushdown Automata 3

4 Ambiguity Recall that we can present CFG derivations as parse trees. Until now this was mere pretty presentation; now it will become important. A context-free grammar G is unambiguous iff every string can be derived by at most one parse tree. G is ambiguous iff there exists any word w L(G) derivable by more than one parse tree. COMP 2600 Context Free Languages and Pushdown Automata 4

5 Example: If-Then and If-Then-Else Consider the CFG S if bexp then S if bexp then S else S prog where bexp and prog stand for boolean expressions and (if-statement free) programs respectively, defined elsewhere. The string if e1 then if e2 then s1 else s2 then has two parse trees: if e1 then if e2 then S S S if e1 then S S else S if e2 then S s1 s2 s1 else S s2 COMP 2600 Context Free Languages and Pushdown Automata 5

6 Example: If-Then and If-Then-Else That grammar was ambiguous. But here s a grammar accepting the exact same language that is unambiguous: S if bexp then S T T if bexp then T else S prog There is now only one parse for if e1 then if e2 then s1 else s2. This is given on the next slide: COMP 2600 Context Free Languages and Pushdown Automata 6

7 Example: If-Then and If-Then-Else if e1 then if e2 then S S T T else You cannot parse this string as if e1 then ( if e2 else s1 ) else s2. s1 S T s2 COMP 2600 Context Free Languages and Pushdown Automata 7

8 Reflecting on This Example We have seen that the same language can be presented with ambiguous and unambiguous grammars. Ambiguity is in general a property of grammars, not of languages. From the point of view of semantics and parsing, it is often desirable to turn an ambiguous grammar into an equivalent unambiguous grammar. This generally involves choices that are not driven by the language itself. E.g. there exists another grammar for the language of the previous slides that is unambiguous and allows the parse if e1 then ( if e2 else s1 ) else s2 but not if e1 then ( if e2 then s1 else s2 ). COMP 2600 Context Free Languages and Pushdown Automata 8

9 What Ambiguity Isn t You might wonder if our grammar is still ambiguous, given the production T if bexp then T else S After a derivation that uses this, we have a choice of whether we expand the T or the S first. Is this an ambiguity? No. A context-free grammar gets its name because non-terminals can be expanded without regard for the context they appear in. In other words, it doesn t matter if you expand T or S first. From the perspective of the parse tree these expansions are happening in parallel. This is a reason why a parse tree is a better formalism for presenting a CFG derivation than listing each step. COMP 2600 Context Free Languages and Pushdown Automata 9

10 Inherently Ambiguous Languages In fact not all context-free languages can be given unambiguous grammars some are inherently ambiguous. Consider the language L = {a i b j c k i = j or j = k} How do we know that this is context-free? First, notice that L = {a i b i c k } {a i b j c j } We then combine CFGs for each side of this union (a standard trick): S T W T UV U aub ε V cv ε W XY X ax ε Y by c ε COMP 2600 Context Free Languages and Pushdown Automata 10

11 Inherently Ambiguous Languages The problem with this CFG is that the union we used has a non-empty intersection, where the a s, b s, and c s all have equal number. The sentences in this intersection are a source of ambiguity: a U U S T V b c V X a X S W Y b Y ε ε ε ε c In fact (not proved here!) there is no alternative choice of grammar for this language that avoids this ambiguity. COMP 2600 Context Free Languages and Pushdown Automata 11

12 The Bad News We would like to have an algorithm that turns grammars into equivalent unambiguous grammars where possible. However this is an uncomputable problem. Worse determining whether a grammar is ambiguous or not in the first place is also uncomputable! Uncomputable problems are everyone in computer science. many more next week in particular. You will see The best response is not despair; rather we should see what tricks and techniques might help us out at least some of the time. COMP 2600 Context Free Languages and Pushdown Automata 12

13 Example: Subtraction Consider the grammar S S S int where int could be any integer and the symbol is intended to be executed as subtraction. This grammar is ambiguous, and this matters: S S S 1 5 S The left tree evaluates to 1; the left evaluates the same string to 3! COMP 2600 Context Free Languages and Pushdown Automata 13

14 Technique 1: Associativity We can remove the ambiguity of a binary infix operator by making it associate to the left or right. S S int int Now can only be read as (5 3) 1 - this is left associativity. For right associativity we would use the production S int S instead. Idea: Force one side of our operator to a lower level, making sure we avoid loops in our grammar that allow the original non-terminal to be recovered. Here we force the right hand side of the minus sign to the lowest possible level a terminal symbol. COMP 2600 Context Free Languages and Pushdown Automata 14

15 Example: Multiplication and Addition S S S S + S int where is to be executed as multiplication and + as addition. Again this is obviously ambiguous could evaluate to 7 or 9. (Note that is also a source of ambiguity as it can be produced by different parse trees, even though it is not ambiguous in the interpretation we have in mind.) If all we care about is resolving ambiguity we can use the same trick as the last slide, making both and + left (or right) associative. But this is not the behaviour we expect from these operations: we expect to have higher precedence than +. COMP 2600 Context Free Languages and Pushdown Automata 15

16 Technique 2: Precedence S S + T T T T int int Given a string 1+2 3, or 2 3+1, we have no choice but to expand to S+T first, so that (thinking bottom-up) the + will be last command to be executed. Suppose we tried to derive by first doing S T T 3. We are then stuck because we cannot send T to 1 + 2! As with associativity this trick works by forcing ourselves down to a lower level to generate parts of our sentences. Here we have three levels: S, then T, then the integers as non-terminals. COMP 2600 Context Free Languages and Pushdown Automata 16

17 Example: Basic Arithmetic S S + T S T T T T U T /U U U (S) int Note that we have brackets available to give a clearly labelled way to loop back to the top if we want to break the usual rules of arithmetic to get the execution (1 + 2) 3 then we must indicate this with explicit brackets. (Note also that the previous slides languages were actually regular and so could have been generated by right-linear grammars. The language above is truly context-free because of the need to keep track of bracket balancing to an arbitrary depth.) COMP 2600 Context Free Languages and Pushdown Automata 17

18 Example: Balanced Brackets The following grammar generates a language where each left bracket is exactly matched by a closing bracket: S ε (S) SS This is ambiguous in a rather stupid way: to generate () we could use the derivation S (S) (), which seems sensible. But we could also start with S SS, then use the left S to derive () as above while sending the right to ε immediately. Or we could have done the opposite. In fact there are infinitely many parse trees for any string in the language! Edit: There is another source of ambiguity here ()()() has two parses even without such abuse of ε. We fix this on the next slide with left associativity. COMP 2600 Context Free Languages and Pushdown Automata 18

19 Technique 3: Controlling ε Instead, we ensure there is only one way to access the empty string: S ε T T TU U U () (T ) The string ε can be derived directly from S, but everything else must go through T. This is another technique that relies on creating a hierarchy of levels within our grammar. ε as a source of ambiguity can be easy to miss, so keep your eyes out for it! COMP 2600 Context Free Languages and Pushdown Automata 19

20 Hierarchy of Automata We have seen that there is a hierarchy of grammars. A language may be generated by grammars at a certain level, but not by less powerful grammars. We have seen that regular (right-linear) grammars are exactly as expressive as finite state automata. Are there notions of automata exactly as expressive as context-free grammars? Context-sensitive grammars? Unrestricted grammars? Yes - there is a hierarchy of automata. Just as we needed a general definition of grammar to define their hierarchy, we need a general view of automata... COMP 2600 Context Free Languages and Pushdown Automata 20

21 General Structure of Automata a0 a1 a an read head input tape Finite State Control Auxiliary Memory The input tape is a sequence of tokens. Each time a symbol is processed the read head advances. The finite state control (FSC) can be in any one of a finite number of states. The auxiliary memory is usually a linear organisation (e.g. a stack or array). COMP 2600 Context Free Languages and Pushdown Automata 21

22 General Automata ctd Each action of the machine may change the FSC state, change the auxiliary memory, or advance to the next input symbol, consuming the current one. The action of the machine depends on the current input symbol, the current FSC state, and the current memory. The machine starts in some particular start state (q 0 ), with the read head at the first input symbol, and the memory containing only the start symbol (Z). A machine accepts an input string as a sentence of the language if it reaches a final state (in F) with the input exhausted (we sometimes also require that the memory be emptied). COMP 2600 Context Free Languages and Pushdown Automata 22

23 Automata and Grammars The kind of auxiliary memory in a machine determines the class of languages that the machine can recognise: Language Class regular context-free context-sensitive unrestricted Memory none stack tape (bounded by input length) unbounded tape We have already looked at Finite State Automata (automata without memory) and their relation to regular languages. We now consider Push-Down Automata (i.e. automata with stack memory) and their relation to context-free grammars and languages. COMP 2600 Context Free Languages and Pushdown Automata 23

24 Push-down Automata PDA a0 a1 a an read head input tape Finite State Control zk z2 z1 stack memory COMP 2600 Context Free Languages and Pushdown Automata 24

25 PDAs ctd Each action of the machine may involve change to the FSC state, pushing or popping the stack, and advancing to the next input symbol. The action of the machine may depend on the current FSC state, the current input symbol, and the current top-of-stack symbol. The machine accepts an input string if it reaches a final state, with the input exhausted, and the stack empty. ASIDE: Other (equivalent) definitions of PDAs exist, where an input string is accepted if the PDA has an empty stack with the input exhausted an input string is accepted if the PDA is in a final state with the input exhausted COMP 2600 Context Free Languages and Pushdown Automata 25

26 Example {a n b n n 1} Recall that this language cannot be recognised by a FSA (because there can only be a finite number of states), but can be generated by a context-free grammar. It can also be recognised by a PDA. Ad hoc design: phase 1: (state q 1 ) push a s from the input onto the stack phase 2: (state q 2 ) pop a s from the stack, if there is a b on input finalise: if the stack is empty and the input is exhausted in the final state (q 3 ), accept the string. COMP 2600 Context Free Languages and Pushdown Automata 26

27 Deterministic PDA Definition A deterministic PDA has the form (Q,q 0,F, Σ, Γ,Z, δ), where Q is the set of states, q 0 Q is the initial state and F Q are the final states; Σ is the alphabet, or set of input symbols; Γ is the set of stack symbols, and Z Γ is the initial stack symbol; δ is a (partial) transition function δ : Q (Σ {ε}) Γ Q Γ δ : (state, input token or ε, top-of-stack) (new state, stack string) such that for all q Q and s Γ, if δ(q,ε,s) is defined then δ(q,a,s) is undefined for all a Σ. This condition is vital to maintain determinism. COMP 2600 Context Free Languages and Pushdown Automata 27

28 Notation Given the transition function δ : Q (Σ {ε}) Γ Q Γ δ : (state, input token or ε, top-of-stack) (new state, stack string) we write δ(q,a,s) = q /σ to show how this function operates. Then the string σ in the result is the string with which we replace the top-ofstack symbol s. (Doing this makes it simple to specify pushes and pops in a uniform way.) We will usually indicate final states by underlining them, e.g. q. COMP 2600 Context Free Languages and Pushdown Automata 28

29 Two types of PDA transition when δ contains (q 1,x,s) q 2 /σ, state input stack the PDA can go from q 1 next symbol is x s on top-of-stack to q 2 x is consumed σ replaces s on stack when δ contains (q 1,ε,s) q 2 /σ, state input stack the PDA can go from q 1 any input, or none s on top-of-stack to q 2 input is ignored σ replaces s on stack Such transitions do not look at or consume an input symbol. COMP 2600 Context Free Languages and Pushdown Automata 29

30 Example ctd Recall that the stack starts off containing only the initial symbol Z. PDA to recognise a n b n (start state q 0, final state(s) underlined): δ(q 0,a,Z) = q 1 /az push first a δ(q 1,a,a) = q 1 /aa push further a s δ(q 1,b,a) = q 2 /ε start popping a s δ(q 2,b,a) = q 2 / ε pop further a s δ(q 2,ε,Z) = q 3 /ε accept Note that δ is a partial function, undefined for many arguments. COMP 2600 Context Free Languages and Pushdown Automata 30

31 Example ctd PDA Trace PDA configurations can be written as a triple (state, remaining input, stack) with the top of stack to the left. (q 0,aaabbb,Z) (q 1,aabbb,aZ) (push first a) (q 1,abbb,aaZ) (q 1,bbb,aaaZ) (q 2,bb,aaZ) (q 2,b,aZ) (q 2,ε,Z) (q 3,ε,ε) (push further a s) (push further a s) (start popping a s) (pop further a s) (pop further a s) (accept) The machine halts in the final state with input exhausted and an empty stack, so the string is accepted. COMP 2600 Context Free Languages and Pushdown Automata 31

32 Example ctd Rejection The string aaba should be rejected by this PDA: (q 0,aaba,Z) (q 1,aba,aZ) (q 1,ba,aaZ) (q 2,a,aZ)??? No transition applies, and the PDA is stuck without reaching a final state. Rejection happens when the transition function is undefined for the current configuration ( that is, state, input symbol and top of stack symbol ). COMP 2600 Context Free Languages and Pushdown Automata 32

33 Example: Palindromes with Centre Mark Consider the language {wcw R w {a,b} w R is w reversed} This is context-free, and we can design a deterministic PDA to accept it: Push a s and b s onto the stack as we seem them; When we see c, change state; Now try to match the tokens we are reading with the tokens on top of the stack, popping as we go; If the top of the stack is the empty stack symbol Z, pop it and enter the final state via an ε-transition. Hopefully our input has been used up too! Full formal details are left as an exercise. COMP 2600 Context Free Languages and Pushdown Automata 33

34 Non-Deterministic PDAs For deterministic PDAs transitions are a (partial) function: δ : Q (Σ {ε}) Γ Q Γ δ : (state, input token or ε, top-of-stack) (new state, stack string) with a side-condition about ε-transitions. For non-deterministic PDAs transitions are a relation δ Q (Σ {ε}) Γ Q Γ with no side-condition. There may be configurations where there are multiple choices of transition. COMP 2600 Context Free Languages and Pushdown Automata 34

35 Non-Deterministic PDAs ctd. Recall for finite state automata, non-determinism can be more convenient, but does not give extra power (thanks to the subset construction). This is not the case for PDAs - non-deterministic PDAs can recognise languages that deterministic PDAs cannot. It turns out that non-deterministic PDAs are the more important from the perspective of Chomsky s hierarchy. COMP 2600 Context Free Languages and Pushdown Automata 35

36 Example: Even-Length Palindromes The language of even length palindromes {ww R w {a,b} w R is w reversed} is context-free but cannot be recognised by a deterministic PDA, because without a centre mark it cannot know whether it is in the first half of a sentence (so should be pushing everything into memory) or the second half (so it should be matching input and stack, and popping). But a non-deterministic PDA can recognise this language via the transition δ(q, ε, x) = r/x where x {a,b,z}, q is the push state, and r the match and pop state. In other words, we continually guess which job we should be doing! COMP 2600 Context Free Languages and Pushdown Automata 36

37 Grammars and PDAs Theorem The class of languages recognised by non-deterministic PDA s is exactly the class of context-free languages. We will only justify this result in one direction: for any CFG, there is a corresponding PDA. This is the most interesting direction since it is the basis of automatically deriving parsers from grammars. (The other direction is a bit complicated). COMP 2600 Context Free Languages and Pushdown Automata 37

38 From CFG to PDA The PDA has three states: q 0 (initial), q 1 (processing), and q 2 (final). alphabet will be that of the CFG, and its stack symbols will be all the CFG s terminals and non-terminals. 1. Initialise the process by pushing the start symbol S onto the stack, and enter state q 1 : δ(q 0,ε,Z) q 1 /SZ Its 2. If a non-terminal is on top of stack, replace it with the right hand side of a production. For all productions A α: δ(q 1,ε,A) q 1 /α continued... COMP 2600 Context Free Languages and Pushdown Automata 38

39 From CFG to PDA, ctd 3. For all terminal symbols t, pop the stack if it matches the input: δ(q 1,t,t) q 1 /ε 4. For termination, add the transition, with final state q 2 : δ(q 1,ε,Z) q 2 /ε In general we get a non-deterministic PDA since there may be several productions for each non-terminal. COMP 2600 Context Free Languages and Pushdown Automata 39

40 Example Derive a PDA for a CFG S S + T T T T U U U (S) int 1. Initialise: δ(q 0,ε,Z) q 1 /SZ 2. Expand non-terminals: δ(q 1,ε,S) q 1 /S + T δ(q 1,ε,T ) q 1 /U δ(q 1,ε,S) q 1 /T δ(q 1,ε,U) q 1 /(S) δ(q 1,ε,T ) q 1 /T U δ(q 1,ε,U) q 1 /int COMP 2600 Context Free Languages and Pushdown Automata 40

41 CFG to PDA ctd 3. Match and pop terminals: δ(q 1,+,+) q 1 /ε δ(q 1,, ) q 1 /ε δ(q 1,int,int) q 1 /ε δ(q 1,(,() q 1 /ε δ(q 1,),)) q 1 /ε 4. Terminate: δ(q 1,ε,Z) q 2 /ε COMP 2600 Context Free Languages and Pushdown Automata 41

42 Example Trace (q 0, int int, Z) (q 1, int int, SZ) (q 1, int int, T Z) (q 1, int int, T UZ) (q 1, int int, U UZ) (q 1, int int, int UZ) (q 1, int, UZ) (q 1, int, UZ) (q 1, int, intz) (q 1, ε, Z) (q 2, ε, ε) accept COMP 2600 Context Free Languages and Pushdown Automata 42

43 A Context-Sensitive Language In case you re curious, a brief look at context-sensitive languages. The following language is not context-free: {a n b n c n n 1} Intuitively, we can imagine a CFG generating either the ab pairs or the bc pairs, but this language requires us to keep the generation process in step, in two different points in the sentential forms. A context-sensitive grammar is on the next slide. Each production has a righthand side of equal or longer length than their left. (If we wanted to include the empty word also, we need a special exemption to this requirement.) COMP 2600 Context Free Languages and Pushdown Automata 43

44 A Context-Sensitive Language ctd. On the left we have a context-sensitive grammar, and on the right an example derivation: S abc S asbc S asbc aabcbc CB BC aabcbc ab ab aabbcc bb bb aabbcc bc bc aabbcc cc cc aabbcc The automata that recognise CSGs have a tape memory, of length bounded by a linear function of the length of the input. COMP 2600 Context Free Languages and Pushdown Automata 44

Models of Computation II: Grammars and Pushdown Automata

Models of Computation II: Grammars and Pushdown Automata Models of Computation II: Grammars and Pushdown Automata COMP1600 / COMP6260 Dirk Pattinson Australian National University Semester 2, 2018 Catch Up / Drop in Lab Session 1 Monday 1100-1200 at Room 2.41

More information

Context Free Languages

Context Free Languages Context Free Languages COMP2600 Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 2016 Slides by Katya Lebedeva and Ranald Clouston. COMP 2600 Context Free

More information

Decision Properties for Context-free Languages

Decision Properties for Context-free Languages Previously: Decision Properties for Context-free Languages CMPU 240 Language Theory and Computation Fall 2018 Context-free languages Pumping Lemma for CFLs Closure properties for CFLs Today: Assignment

More information

Pushdown Automata. A PDA is an FA together with a stack.

Pushdown Automata. A PDA is an FA together with a stack. Pushdown Automata A PDA is an FA together with a stack. Stacks A stack stores information on the last-in firstout principle. Items are added on top by pushing; items are removed from the top by popping.

More information

14.1 Encoding for different models of computation

14.1 Encoding for different models of computation Lecture 14 Decidable languages In the previous lecture we discussed some examples of encoding schemes, through which various objects can be represented by strings over a given alphabet. We will begin this

More information

Syntax Analysis Part I

Syntax Analysis Part I Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,

More information

Multiple Choice Questions

Multiple Choice Questions Techno India Batanagar Computer Science and Engineering Model Questions Subject Name: Formal Language and Automata Theory Subject Code: CS 402 Multiple Choice Questions 1. The basic limitation of an FSM

More information

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : III Year, V Semester Section : CSE - 1 & 2 Subject Code : CS6503 Subject

More information

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Language Definition Problem How to precisely

More information

Introduction to Lexing and Parsing

Introduction to Lexing and Parsing Introduction to Lexing and Parsing ECE 351: Compilers Jon Eyolfson University of Waterloo June 18, 2012 1 Riddle Me This, Riddle Me That What is a compiler? 1 Riddle Me This, Riddle Me That What is a compiler?

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016 Lecture 15 Ana Bove May 23rd 2016 More on Turing machines; Summary of the course. Overview of today s lecture: Recap: PDA, TM Push-down

More information

CT32 COMPUTER NETWORKS DEC 2015

CT32 COMPUTER NETWORKS DEC 2015 Q.2 a. Using the principle of mathematical induction, prove that (10 (2n-1) +1) is divisible by 11 for all n N (8) Let P(n): (10 (2n-1) +1) is divisible by 11 For n = 1, the given expression becomes (10

More information

Ambiguous Grammars and Compactification

Ambiguous Grammars and Compactification Ambiguous Grammars and Compactification Mridul Aanjaneya Stanford University July 17, 2012 Mridul Aanjaneya Automata Theory 1/ 44 Midterm Review Mathematical Induction and Pigeonhole Principle Finite Automata

More information

Turing Machines. A transducer is a finite state machine (FST) whose output is a string and not just accept or reject.

Turing Machines. A transducer is a finite state machine (FST) whose output is a string and not just accept or reject. Turing Machines Transducers: A transducer is a finite state machine (FST) whose output is a string and not just accept or reject. Each transition of an FST is labeled with two symbols, one designating

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 11 Ana Bove April 26th 2018 Recap: Regular Languages Decision properties of RL: Is it empty? Does it contain this word? Contains

More information

A Characterization of the Chomsky Hierarchy by String Turing Machines

A Characterization of the Chomsky Hierarchy by String Turing Machines A Characterization of the Chomsky Hierarchy by String Turing Machines Hans W. Lang University of Applied Sciences, Flensburg, Germany Abstract A string Turing machine is a variant of a Turing machine designed

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

More information

Specifying Syntax COMP360

Specifying Syntax COMP360 Specifying Syntax COMP360 The most important thing in the programming language is the name. A language will not succeed without a good name. I have recently invented a very good name and now I am looking

More information

Languages and Compilers

Languages and Compilers Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 2012-13 3. Formal Languages, Grammars and Automata Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office:

More information

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Massachusetts Institute of Technology Language Definition Problem How to precisely define language Layered structure

More information

Compiler Construction

Compiler Construction Compiler Construction Exercises 1 Review of some Topics in Formal Languages 1. (a) Prove that two words x, y commute (i.e., satisfy xy = yx) if and only if there exists a word w such that x = w m, y =

More information

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG) CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG) Objectives Introduce Pushdown Automaton (PDA) Show that PDA = CFG In terms of descriptive power Pushdown Automaton (PDA) Roughly

More information

JNTUWORLD. Code No: R

JNTUWORLD. Code No: R Code No: R09220504 R09 SET-1 B.Tech II Year - II Semester Examinations, April-May, 2012 FORMAL LANGUAGES AND AUTOMATA THEORY (Computer Science and Engineering) Time: 3 hours Max. Marks: 75 Answer any five

More information

Defining syntax using CFGs

Defining syntax using CFGs Defining syntax using CFGs Roadmap Last time Defined context-free grammar This time CFGs for specifying a language s syntax Language membership List grammars Resolving ambiguity CFG Review G = (N,Σ,P,S)

More information

Decidable Problems. We examine the problems for which there is an algorithm.

Decidable Problems. We examine the problems for which there is an algorithm. Decidable Problems We examine the problems for which there is an algorithm. Decidable Problems A problem asks a yes/no question about some input. The problem is decidable if there is a program that always

More information

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser

More information

Syntax Analysis Check syntax and construct abstract syntax tree

Syntax Analysis Check syntax and construct abstract syntax tree Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error reporting and recovery Model using context free grammars Recognize using Push down automata/table Driven Parsers

More information

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

Theory of Computation Dr. Weiss Extra Practice Exam Solutions Name: of 7 Theory of Computation Dr. Weiss Extra Practice Exam Solutions Directions: Answer the questions as well as you can. Partial credit will be given, so show your work where appropriate. Try to be

More information

Optimizing Finite Automata

Optimizing Finite Automata Optimizing Finite Automata We can improve the DFA created by MakeDeterministic. Sometimes a DFA will have more states than necessary. For every DFA there is a unique smallest equivalent DFA (fewest states

More information

Theory of Computations Spring 2016 Practice Final Exam Solutions

Theory of Computations Spring 2016 Practice Final Exam Solutions 1 of 8 Theory of Computations Spring 2016 Practice Final Exam Solutions Name: Directions: Answer the questions as well as you can. Partial credit will be given, so show your work where appropriate. Try

More information

LL(1) predictive parsing

LL(1) predictive parsing LL(1) predictive parsing Informatics 2A: Lecture 11 Mary Cryan School of Informatics University of Edinburgh mcryan@staffmail.ed.ac.uk 10 October 2018 1 / 15 Recap of Lecture 10 A pushdown automaton (PDA)

More information

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees Derivations of a CFG MACM 300 Formal Languages and Automata Anoop Sarkar http://www.cs.sfu.ca/~anoop strings grow on trees strings grow on Noun strings grow Object strings Verb Object Noun Verb Object

More information

CSCI312 Principles of Programming Languages!

CSCI312 Principles of Programming Languages! CSCI312 Principles of Programming Languages!! Chapter 3 Regular Expression and Lexer Xu Liu Recap! Copyright 2006 The McGraw-Hill Companies, Inc. Clite: Lexical Syntax! Input: a stream of characters from

More information

COMP 330 Autumn 2018 McGill University

COMP 330 Autumn 2018 McGill University COMP 330 Autumn 2018 McGill University Assignment 4 Solutions and Grading Guide Remarks for the graders appear in sans serif font. Question 1[25 points] A sequence of parentheses is a sequence of ( and

More information

CS 44 Exam #2 February 14, 2001

CS 44 Exam #2 February 14, 2001 CS 44 Exam #2 February 14, 2001 Name Time Started: Time Finished: Each question is equally weighted. You may omit two questions, but you must answer #8, and you can only omit one of #6 or #7. Circle the

More information

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar Code No: R05310501 Set No. 1 III B.Tech I Semester Regular Examinations, November 2008 FORMAL LANGUAGES AND AUTOMATA THEORY (Computer Science & Engineering) Time: 3 hours Max Marks: 80 Answer any FIVE

More information

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function TAFL 1 (ECS-403) Unit- V 5.1 Turing Machine 5.2 TM as computer of Integer Function 5.2.1 Simulating Turing Machine by Computer 5.2.2 Simulating Computer by Turing Machine 5.3 Universal Turing Machine 5.4

More information

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis CS 1622 Lecture 2 Lexical Analysis CS 1622 Lecture 2 1 Lecture 2 Review of last lecture and finish up overview The first compiler phase: lexical analysis Reading: Chapter 2 in text (by 1/18) CS 1622 Lecture

More information

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5 1 Not all languages are regular So what happens to the languages which are not regular? Can we still come up with a language recognizer?

More information

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters : Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Scanner Parser Static Analyzer Intermediate Representation Front End Back End Compiler / Interpreter

More information

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama Print family (or last) name: Print given (or first) name: I have read and understand all of

More information

KEY. A 1. The action of a grammar when a derivation can be found for a sentence. Y 2. program written in a High Level Language

KEY. A 1. The action of a grammar when a derivation can be found for a sentence. Y 2. program written in a High Level Language 1 KEY CS 441G Fall 2018 Exam 1 Matching: match the best term from the following list to its definition by writing the LETTER of the term in the blank to the left of the definition. (1 point each) A Accepts

More information

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016 University of Nevada, Las Vegas Computer Science 456/656 Fall 2016 The entire examination is 925 points. The real final will be much shorter. Name: No books, notes, scratch paper, or calculators. Use pen

More information

1. [5 points each] True or False. If the question is currently open, write O or Open.

1. [5 points each] True or False. If the question is currently open, write O or Open. University of Nevada, Las Vegas Computer Science 456/656 Spring 2018 Practice for the Final on May 9, 2018 The entire examination is 775 points. The real final will be much shorter. Name: No books, notes,

More information

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

More information

Introduction to Syntax Analysis

Introduction to Syntax Analysis Compiler Design 1 Introduction to Syntax Analysis Compiler Design 2 Syntax Analysis The syntactic or the structural correctness of a program is checked during the syntax analysis phase of compilation.

More information

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form Normal Forms for CFG s Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form 1 Variables That Derive Nothing Consider: S -> AB, A -> aa a, B -> AB Although A derives

More information

Introduction to Syntax Analysis. The Second Phase of Front-End

Introduction to Syntax Analysis. The Second Phase of Front-End Compiler Design IIIT Kalyani, WB 1 Introduction to Syntax Analysis The Second Phase of Front-End Compiler Design IIIT Kalyani, WB 2 Syntax Analysis The syntactic or the structural correctness of a program

More information

PDA s. and Formal Languages. Automata Theory CS 573. Outline of equivalence of PDA s and CFG s. (see Theorem 5.3)

PDA s. and Formal Languages. Automata Theory CS 573. Outline of equivalence of PDA s and CFG s. (see Theorem 5.3) CS 573 Automata Theory and Formal Languages Professor Leslie Lander Lecture # 20 November 13, 2000 Greibach Normal Form (GNF) Sheila Greibach s normal form (GNF) for a CFG is one where EVERY production

More information

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309 PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

More information

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half. Homework Context Free Languages Homework #2 returned Homework #3 due today Homework #4 Pg 133 -- Exercise 1 (use structural induction) Pg 133 -- Exercise 3 Pg 134 -- Exercise 8b,c,d Pg 135 -- Exercise

More information

Compiler Design Concepts. Syntax Analysis

Compiler Design Concepts. Syntax Analysis Compiler Design Concepts Syntax Analysis Introduction First task is to break up the text into meaningful words called tokens. newval=oldval+12 id = id + num Token Stream Lexical Analysis Source Code (High

More information

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table. Code No: XXXXX JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY, HYDERABAD B.Tech II Year I Semester Examinations (Common to CSE and IT) Note: This question paper contains two parts A and B. Part A is compulsory

More information

15 212: Principles of Programming. Some Notes on Grammars and Parsing

15 212: Principles of Programming. Some Notes on Grammars and Parsing 15 212: Principles of Programming Some Notes on Grammars and Parsing Michael Erdmann Spring 2011 1 Introduction These notes are intended as a rough and ready guide to grammars and parsing. The theoretical

More information

CpSc 421 Final Solutions

CpSc 421 Final Solutions CpSc 421 Final Solutions Do any eight of the ten problems below. If you attempt more than eight problems, please indicate which ones to grade (otherwise we will make a random choice). This allows you to

More information

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION CSE 105 THEORY OF COMPUTATION Spring 2018 http://cseweb.ucsd.edu/classes/sp18/cse105-ab/ Today's learning goals Sipser Section 2.2 Define push-down automata informally and formally Trace the computation

More information

Recursively Enumerable Languages, Turing Machines, and Decidability

Recursively Enumerable Languages, Turing Machines, and Decidability Recursively Enumerable Languages, Turing Machines, and Decidability 1 Problem Reduction: Basic Concepts and Analogies The concept of problem reduction is simple at a high level. You simply take an algorithm

More information

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino 3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the

More information

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast! Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast! Compiler Passes Analysis of input program (front-end) character stream

More information

Structure of a Compiler: Scanner reads a source, character by character, extracting lexemes that are then represented by tokens.

Structure of a Compiler: Scanner reads a source, character by character, extracting lexemes that are then represented by tokens. CS 441 Fall 2018 Notes Compiler - software that translates a program written in a source file into a program stored in a target file, reporting errors when found. Source Target program written in a High

More information

LR Parsing. The first L means the input string is processed from left to right.

LR Parsing. The first L means the input string is processed from left to right. LR Parsing 1 Introduction The LL Parsing that is provided in JFLAP is what is formally referred to as LL(1) parsing. Grammars that can be parsed using this algorithm are called LL grammars and they form

More information

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which: P R O B L E M S Finite Autom ata. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which: a) Are a multiple of three in length. b) End with the string

More information

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007. Closure Properties of CFLs; Introducing TMs CS154 Chris Pollett Apr 9, 2007. Outline Closure Properties of Context Free Languages Algorithms for CFLs Introducing Turing Machines Closure Properties of CFL

More information

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and Computer Language Theory Chapter 4: Decidability 1 Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

More information

Assignment 4 CSE 517: Natural Language Processing

Assignment 4 CSE 517: Natural Language Processing Assignment 4 CSE 517: Natural Language Processing University of Washington Winter 2016 Due: March 2, 2016, 1:30 pm 1 HMMs and PCFGs Here s the definition of a PCFG given in class on 2/17: A finite set

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grammars Describing Languages We've seen two models for the regular languages: Finite automata accept precisely the strings in the language. Regular expressions describe precisely the strings

More information

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006. Context Free Grammars CS154 Chris Pollett Mar 1, 2006. Outline Formal Definition Ambiguity Chomsky Normal Form Formal Definitions A context free grammar is a 4-tuple (V, Σ, R, S) where 1. V is a finite

More information

CS 314 Principles of Programming Languages. Lecture 3

CS 314 Principles of Programming Languages. Lecture 3 CS 314 Principles of Programming Languages Lecture 3 Zheng Zhang Department of Computer Science Rutgers University Wednesday 14 th September, 2016 Zheng Zhang 1 CS@Rutgers University Class Information

More information

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict Parsing - 1 What is parsing? Shift-reduce parsing Shift-reduce conflict Reduce-reduce conflict Operator precedence parsing Parsing-1 BGRyder Spring 99 1 Parsing Parsing is the reverse of doing a derivation

More information

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6 Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,

More information

Definition 2.8: A CFG is in Chomsky normal form if every rule. only appear on the left-hand side, we allow the rule S ǫ.

Definition 2.8: A CFG is in Chomsky normal form if every rule. only appear on the left-hand side, we allow the rule S ǫ. CS533 Class 02b: 1 c P. Heeman, 2017 CNF Pushdown Automata Definition Equivalence Overview CS533 Class 02b: 2 c P. Heeman, 2017 Chomsky Normal Form Definition 2.8: A CFG is in Chomsky normal form if every

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter

More information

Theory Bridge Exam Example Questions Version of June 6, 2008

Theory Bridge Exam Example Questions Version of June 6, 2008 Theory Bridge Exam Example Questions Version of June 6, 2008 This is a collection of sample theory bridge exam questions. This is just to get some idea of the format of the bridge exam and the level of

More information

Part 5 Program Analysis Principles and Techniques

Part 5 Program Analysis Principles and Techniques 1 Part 5 Program Analysis Principles and Techniques Front end 2 source code scanner tokens parser il errors Responsibilities: Recognize legal programs Report errors Produce il Preliminary storage map Shape

More information

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7 Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

More information

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University Compilation 2012 Parsers and Scanners Jan Midtgaard Michael I. Schwartzbach Aarhus University Context-Free Grammars Example: sentence subject verb object subject person person John Joe Zacharias verb asked

More information

Limits of Computation p.1/?? Limits of Computation p.2/??

Limits of Computation p.1/?? Limits of Computation p.2/?? Context-Free Grammars (CFG) There are languages, such as {0 n 1 n n 0} that cannot be described (specified) by finite automata or regular expressions. Context-free grammars provide a more powerful mechanism

More information

Theory and Compiling COMP360

Theory and Compiling COMP360 Theory and Compiling COMP360 It has been said that man is a rational animal. All my life I have been searching for evidence which could support this. Bertrand Russell Reading Read sections 2.1 3.2 in the

More information

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

QUESTION BANK. Formal Languages and Automata Theory(10CS56) QUESTION BANK Formal Languages and Automata Theory(10CS56) Chapter 1 1. Define the following terms & explain with examples. i) Grammar ii) Language 2. Mention the difference between DFA, NFA and εnfa.

More information

Final Course Review. Reading: Chapters 1-9

Final Course Review. Reading: Chapters 1-9 Final Course Review Reading: Chapters 1-9 1 Objectives Introduce concepts in automata theory and theory of computation Identify different formal language classes and their relationships Design grammars

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee 1 What Is a Programming Language? Language = syntax + semantics The syntax of a language is concerned with the form of a program: how

More information

Properties of Regular Expressions and Finite Automata

Properties of Regular Expressions and Finite Automata Properties of Regular Expressions and Finite Automata Some token patterns can t be defined as regular expressions or finite automata. Consider the set of balanced brackets of the form [[[ ]]]. This set

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

More information

Variants of Turing Machines

Variants of Turing Machines November 4, 2013 Robustness Robustness Robustness of a mathematical object (such as proof, definition, algorithm, method, etc.) is measured by its invariance to certain changes Robustness Robustness of

More information

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Compilers and computer architecture From strings to ASTs (2): context free grammars

Compilers and computer architecture From strings to ASTs (2): context free grammars 1 / 1 Compilers and computer architecture From strings to ASTs (2): context free grammars Martin Berger October 2018 Recall the function of compilers 2 / 1 3 / 1 Recall we are discussing parsing Source

More information

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity CMSC 330: Organization of Programming Languages Context-Free Grammars Ambiguity Review Why should we study CFGs? What are the four parts of a CFG? How do we tell if a string is accepted by a CFG? What

More information

CS 321 Programming Languages and Compilers. VI. Parsing

CS 321 Programming Languages and Compilers. VI. Parsing CS 321 Programming Languages and Compilers VI. Parsing Parsing Calculate grammatical structure of program, like diagramming sentences, where: Tokens = words Programs = sentences For further information,

More information

ECS 120 Lesson 16 Turing Machines, Pt. 2

ECS 120 Lesson 16 Turing Machines, Pt. 2 ECS 120 Lesson 16 Turing Machines, Pt. 2 Oliver Kreylos Friday, May 4th, 2001 In the last lesson, we looked at Turing Machines, their differences to finite state machines and pushdown automata, and their

More information

CS 441G Fall 2018 Exam 1 Matching: LETTER

CS 441G Fall 2018 Exam 1 Matching: LETTER CS 441G Fall 2018 Exam 1 Matching: match the best term from the following list to its definition by writing the LETTER of the term in the blank to the left of the definition. All 31 definitions are given

More information

CS 125 Section #4 RAMs and TMs 9/27/16

CS 125 Section #4 RAMs and TMs 9/27/16 CS 125 Section #4 RAMs and TMs 9/27/16 1 RAM A word-ram consists of: A fixed set of instructions P 1,..., P q. Allowed instructions are: Modular arithmetic and integer division on registers; the standard

More information

Computability via Recursive Functions

Computability via Recursive Functions Computability via Recursive Functions Church s Thesis All effective computational systems are equivalent! To illustrate this point we will present the material from chapter 4 using the partial recursive

More information

UNIT I PART A PART B

UNIT I PART A PART B OXFORD ENGINEERING COLLEGE (NAAC ACCREDITED WITH B GRADE) DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING LIST OF QUESTIONS YEAR/SEM: III/V STAFF NAME: Dr. Sangeetha Senthilkumar SUB.CODE: CS6503 SUB.NAME:

More information

ECS 120 Lesson 7 Regular Expressions, Pt. 1

ECS 120 Lesson 7 Regular Expressions, Pt. 1 ECS 120 Lesson 7 Regular Expressions, Pt. 1 Oliver Kreylos Friday, April 13th, 2001 1 Outline Thus far, we have been discussing one way to specify a (regular) language: Giving a machine that reads a word

More information

Turing Machine Languages

Turing Machine Languages Turing Machine Languages Based on Chapters 23-24-25 of (Cohen 1997) Introduction A language L over alphabet is called recursively enumerable (r.e.) if there is a Turing Machine T that accepts every word

More information

LL(1) predictive parsing

LL(1) predictive parsing LL(1) predictive parsing Informatics 2A: Lecture 11 John Longley School of Informatics University of Edinburgh jrl@staffmail.ed.ac.uk 13 October, 2011 1 / 12 1 LL(1) grammars and parse tables 2 3 2 / 12

More information

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever Automata & languages A primer on the Theory of Computation The imitation game (24) Benedict Cumberbatch Alan Turing (92-954) Laurent Vanbever www.vanbever.eu ETH Zürich (D-ITET) September, 2 27 Brief CV

More information