Logic Language A Context Free Grammar To define a Language For which every string is a logic expression.

Slides:



Advertisements
Similar presentations
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Arithmetic Expressions
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Definitions Synthesized Attributes
Languages & Strings String Operations Language Definitions.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Languages A Language L over a finite alphabet  is a set of strings of characters from the alphabet. Examples are : 1.L = { s | s is a grammatical English.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
Languages A Language L over a finite alphabet  is a set of strings of characters from the alphabet. Examples are : 1.L = { s | s is a grammatical English.
Context-Free Grammars
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
PART I: overview material
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Syntax and Semantics.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
A Programming Languages Syntax Analysis (1)
LESSON 04.
Overview of Previous Lesson(s) Over View  Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. 
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Syntax Analyzer (Parser)
10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter2 : Lexical Analysis
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
PROGRAMMING LANGUAGES
Compiler Construction
Context-Free Grammars
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
CSC 4181Compiler Construction Context-Free Grammars
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
Context-Free Grammars
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Context-Free Grammars
Context Free Grammars-II
Visual Programming Languages ICS 539 Icon System Visual Languages & Visual Programming, Chapter 1, Editor Chang, 1990 ICS Department KFUPM Sept. 1,
Presentation transcript:

Logic Language A Context Free Grammar To define a Language For which every string is a logic expression

Example Strings in Logic Language Strings in Logic Language Example 1 x = y   x  z. Example 2 x = u  (w  v) ; y = u  x. Source strings are processed by a lexical analyser which replaces variable names such as a,b,.. x,y,z with a token where i is index of lower case letter in “a,b,.. x,y,z ”. Lexical analysis replaces logical constants such as “T” and “F” with where i is the index of constant in “T,F”. Operators are replaced as follows :  by ~,  by *,  by + and remaining operator symbols =,(,) are left as is.

Logic Language Grammar Grammar Rules N  SM. M  ;S M  S  v=E E  TR R  +E R  T  FU U  *T U  F  ~F F  (E) F  v F  c Derivation of v=v;v=v+V. N  SM.  v=EM.  v=TRM.  v=FURM.  v=vURM.  v=vM.  v=v;S.  v=v;S.  v=v;v=OR.  v=v;v=TUR.  v=v;v=FUR.  v=v;v=vUR....

First Sets N:v M:;# S:v E:vc~( R:+# U:*# T:vc~( F:vc~( Follow Sets N:$ M:. S:;$. E:;$.) R:;$.) U:+;$.) T:+;$.) F:*+;$.) Rule Signatures 1:v 2:; 3:. 4:v 5:vc~( 6:+ 7:;$.) 8:vc~( 9:* 10:+;$.) 11:v 12:c 13:~ 14:( Nonterminal- Terminal-Rule Triples Nv1 M;2 M.3 Sv4 Ev5 Ec5 E~5 E(5 R+6 R;7 R$7 R.7 R)7 Tv8 Tc8 T~8 T(8 U*9 U+10 U;10 U$10 U.10 U)10 Fv11 Fc12 F~13 F(14

N | v=v+v*v.$ | Replace N by R1 : SM. | v=v+v*v.$ | Replace S by R4 : v=EM. | v=v+v*v.$ | Match : =EM. | =v+v*v.$ | v Match : EM. | v+v*v.$ | v= Replace E by R5 : TRM. | v+v*v.$ | v= Replace E by R8 : FURM. | v+v*v.$ | v= Replace R by R11 : vURM. | v+v*v.$ | v= Match : URM. | +v*v.$ | v=v Replace R by R10 : RM. | +v*v.$ | v=v Replace E by R6 : +EM. | +v*v.$ | v=v Match : EM. | v*v.$ | v=v+ Replace E by R5 : TRM. | v*v.$ | v=v+ Replace E by R8 : FURM. | v*v.$ | v=v+ Replace R by R11 : vURM. | v*v.$ | v=v+ Match : URM. | *v.$ | v=v+v Replace R by R9 : *TRM. | *v.$ | v=v+v Match : TRM. | v.$ | v=v+v* Replace E by R8 : FURM. | v.$ | v=v+v* Replace R by R11 : vURM. | v.$ | v=v+v* Match : URM. |.$ | v=v+v*v NSM SvE= ETR TFU Fv U# RE+ ETR TFU Fv UT* TFU Fv U# R# M# M - R - U - F - v T - v U - v* F - v T - vv* E - vv* R - vv*+ U - F - v T - v E - vvv*+ S - vvvv*+= N - vvvv*+=

Grammar N -> SM. M -> ;N M -> # S -> OR R -> +S R -> # O -> TU U -> *T U -> # T -> F T -> ~F F -> (S) F -> v F -> c First Sets N:~(vc M:;# S:~(vc R:+# O:~(vc U:*# T:~(vc F:(vc Follow Sets N:$. M:. S:;$). R:;$). O:+;$). U:+;$). T:*+;$). F:*+;$). Rule Signatures 1:~(vc 2:; 3:. 4:~(vc 5:+ 6:;$). 7:~(vc 8:* 9:+;$). 10:(vc 11:~ 12:( 13:v 14:c Triples N~1 N(1 Nv1 Nc1 M;2 M.3 S~4 S(4 Sv4 Sc4 R+5 R;6 R$6 R)6 R.6 Triples-Cont O~7 O(7 Ov7 Oc7 U*8 U+9 U;9 U$9 U)9 U.9 T(10 Tv10 Tc10 T~11 F(12 Fv13 Fc14

Postfix : Infix Expression - w = x * (y + ~z), Postfix Expression - w x y z ~ + * = xyzw 100? Operand Stack Next Postfix Character w wx w xy w x yz w x y z~

Postfix : Infix Expression - w = x * (y + ~z), Postfix Expression - w x y z ~ + * = xyzw 100? Operand Stack Next Postfix Character w x y 1+ w x 1* w 1 1 1

Attributed Grammars An attribute is a record associated with a grammar character; it can be used for generating target code. In example, A is postfix string. Rule #RuleSemantic ActionExplanation 1 E A  T A R A A 0 = A 1 & A 2 Concatenate postfix 2 R A  + T A R A A 0 = A 2 & A 3 & “+” Insert + after 2 nd operand 3 R A  A 0 = Postfix empty 4 T A  a U A A 0 = “a” & A 2 Operands in infix order 5 U A  * a U A A 0 = “a” & “*” & A 3 Insert * after 2 nd operand 6 U A  A 0 = Postfix empty

Sample Parse

Lexical Analysis In order to partition the processing involved in translating an expression in logic language (representing a circuit) into an internal representation suitable for simulating the circuit, the processing is divided into several stages: Lexical Analysis source code is converted into tokens consisting of a syntactic code used in parsing and a semantic code used to generate target language an “a” is converted to a token where the syntactic code “d” signifies data and the semantic code “1” indicates where the value can be accessed. Possible variables are denoted by “abcd..z” with semantic code being offset from “a”. Constants “T” and “F” are converted to and a “+” is converted to a token, “*” to, “~” to and “=“ to

Lexical Analysis - continued Syntax directed parsing & translation into postfix Tokens are parsed where the syntactic codes are terminals in the grammar Postfix excecution to test the behaviour of the circuit. Postfix evaluation

Parse a+a*a$ E | a+a*a$ | X by R1 : TR | a+a*a$ | X by R4 : aUR | a+a*a$ | Match : UR | +a*a$ | a X by R6 : R | +a*a$ | a X by R2 : +TR | +a*a$ | a Match : TR | a*a$ | a+ X by R4 : aUR | a*a$ | a+ Match : UR | *a$ | a+a X by R5 : *aUR | *a$ | a+a Match : aUR | a$ | a+a* Match : UR | $ | a+a*a X by R6 : R | $ | a+a*a X by R3 : | $ | a+a*a Match : | | a+a*a$ Attribute Evaluation X by R3 : | $ | a+a*a R = X by R6 : R | $ | a+a*a U = X by R5 : *aUR | *a$ | a+a U = a 3 * X by R4 : aUR | a*a$ | a+ T = a 2 a 3 * X by R2 : +TR | +a*a$ | a R = a 2 a 3 * + X by R6 : R | +a*a$ | a U = X by R4 : aUR | a+a*a$ | T = a 1 a 2 a 3 * + X by R1 : TR | a+a*a$ | E = a 1 a 2 a 3 * + Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & A 3 & “+” 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 =

Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & “+” & A 3 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 = E T R a U + T R * a U a U a a * a * a a + a a * a + Syntax Directed Translation of Infix to Postfix

Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & “+” & A 3 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 = E T R a U + T R * a U a U a a * a * a a + a a * a + E 1 = T 2 R 3 T 2 = aU 4 U 4 = *aU 5 U 5 = R 3 = +T 6 R 7 T 6 = aU 8 U 8 = R 7 =

LL(1) Syntax Directed Translation Infix to Postfix Parsing: – Start with the nonterminal representing the language as the unmatched sentential form (USF) and also the postfix output (PO) – Repeat remaining source string is empty or until failure Let X be the leftmost character in USF, t leftmost character of remaining source If X is terminal it must the first character of the remaining source (otherwise failure) If X is nonterminal then –Select the rule R k where k is Parse table entry in row X, column t and apply this rule to X –Let X be the leftmost nonterminal in PO and apply the semantic action S k to it.

Rule #RuleSemantic Action 1 E A  T A R A A 0 = A 1 & A 2 2 R A  + T A R A A 0 = A 2 & “+” & A 3 3 R A  A 0 = 4 T A  a U A A 0 = “a” & A 2 5 U A  * a U A A 0 = “a” & “*” & A 3 6 U A  A 0 = E1 T2 R3 a U4 + T6 R7 * a U5 a U8 a a * a *a a + a a * a + E 1 = T 2 R 3 T 2 = aU 4 U 4 = a*U 5 U 5 = R 3 = +T 6 R 7 T 6 = aU 8 U 8 = R 7 = P = E1 = T2R3 = aU4R3 = a a*U5 R3 = a a*R3 = a a * T6 + R7 = a a * aU8 + R7 = a a * a + R7 = a a * a +

GrammarParse v=v*vOutputPostfix 1. N  SM. 2. M  ;S 3. M  # 4. S  v=E 5. E  TR 6. R  +E 7. R  # 8. T  FU 9. U  *T 10. U  # 11. F  v 12. F  c 13. F  ~F 14. F  (E) N | v=v*v.$ | Replace N by R1 : SM. | v=v*v.$ | Replace S by R4 : v=EM. | v=v*v.$ | Match : =EM. | =v*v.$ | v Match : EM. | v*v.$ | v= Replace E by R5 : TRM. | v*v.$ | v= Replace E by R8 : FURM. | v*v.$ | v= Replace R by R11 : vURM. | v*v.$ | v= Match : URM. | *v.$ | v=v Replace R by R9 : *TRM. | *v.$ | v=v Match : TRM. | v.$ | v=v* Replace E by R8 : FURM. | v.$ | v=v* Replace R by R11 : vURM. | v.$ | v=v* Match : URM. |.$ | v=v*v Replace R by R10 : RM. |.$ | v=v*v Replace E by R7 : M. |.$ | v=v*v Replace M by R3 :. |.$ | v=v*v Match : | $ | v=v*v. NSM SvE= ETR TFU Fv UT* TFU Fv U# R# M# M - R - U - F - v T - v U - v* F - v T - vv* E - vv* S - vvv*= N - vvv*=

GrammarParse v=v*vOutput Postfix 1. N  SM. 2. M  ;S 3. M  # 4. S  v=E 5. E  TR 6. R  +E 7. R  # 8. T  FU 9. U  *T 10. U  # 11. F  v 12. F  c 13. F  ~F 14. F  (E) N | v=v+v*v.$ | Replace N by R1 : SM. | v=v+v*v.$ | Replace S by R4 : v=EM. | v=v+v*v.$ | Match : =EM. | =v+v*v.$ | v Match : EM. | v+v*v.$ | v= Replace E by R5 : TRM. | v+v*v.$ | v= Replace E by R8 : FURM. | v+v*v.$ | v= Replace R by R11 : vURM. | v+v*v.$ | v= Match : URM. | +v*v.$ | v=v Replace R by R10 : RM. | +v*v.$ | v=v Replace E by R6 : +EM. | +v*v.$ | v=v Match : EM. | v*v.$ | v=v+ Replace E by R5 : TRM. | v*v.$ | v=v+ Replace E by R8 : FURM. | v*v.$ | v=v+ Replace R by R11 : vURM. | v*v.$ | v=v+ Match : URM. | *v.$ | v=v+v Replace R by R9 : *TRM. | *v.$ | v=v+v Match : TRM. | v.$ | v=v+v* Replace E by R8 : FURM. | v.$ | v=v+v* Replace R by R11 : vURM. | v.$ | v=v+v* Match : URM. |.$ | v=v+v*v Replace R by R10 : RM. |.$ | v=v+v*v Replace E by R7 : M. |.$ | v=v+v*v Replace M by R3 :. |.$ | v=v+v*v Match : | $ | v=v+v*v. R 1 => "NSM" R 2 => "M;S" R 3 => "M#" R 4 => "SvE=" R 5 => "ETR" R 6 => "RE+" R 7 => "R#" R 8 => "TFU" R 9 => "UT*" R 10 => "U#" R 11 => "Fv" R 12 => "Fc" R 13 => "FF~" R 14 => "FE“ Output is LHS Followed by Postfix of RHS NSM SvE= ETR TFU Fv U# RE+ ETR TFU Fv UT* TFU Fv U# R# M# M - R - U - F - v T - v U - v* F - v T - vv* E - vv* R - vv*+ U - F - v T - v E - vvv*+ S - vvvv*+= N - vvvv*+=