






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This lecture was delivered by Ruiz Pereira at Chandra Shekhar Azad University of Agriculture
Typology: Slides
1 / 30
This page cannot be seen from the preview
Don't miss anything!
(ident) "val"
(assign)
(number) 10
(times)
(ident) "val"
(plus)
(ident) "i"
ident = number * ident + ident
Term
Expression
Statement
John Backus : developed the first Fortran compiler Peter Naur : edited the Algol60 report
literal
terminal symbol
nonterminal symbol
terminates a production
left-hand side right-hand side
Expr = ["+" | "-"] Term {("+" | "-") Term}. Term = Factor {("*" | "/") Factor}. Factor = ident | number | "(" Expr ")".
scanner
parser
main
>coco Sample.atg Coco/R (Aug 22, 2006) checking parser + scanner generated 0 errors detected
Sample = "red" "apple" | "orange".
COMPILER Sample PRODUCTIONS Sample = "red" "apple" | "orange". END Sample.
class Parser { ... void Sample () { if (la.kind == 1) { Get(); Expect(2); } else if (la.kind == 3) { Get(); } else SynErr(5); } ... Token la ; // lookahead token void Get () { la = Scanner.Scan(); ... } void Expect (int n) { if (la.kind == n) Get(); else SynErr(n); } public void Parse () { Get(); Sample(); } ... }
Sample = "red" "apple" | "orange".
token codes returned by the scanner
COMPILER Sample CHARACTERS digit = '0'..'9'. TOKENS number = digit {digit}. IGNORE '\r' + '\n' PRODUCTIONS Sample = {"calc" Expr}. Expr = Term {'+' Term}. Term = number. END Sample.
**>coco Sample.atg
csc Compile.cs Scanner.cs Parser.cs Compile Input.txt**
class Parser { ... void Sample () { int n; while (la.kind == 2) { Get(); Expr(out n); Console.WriteLine(n); } } void Expr (out int n) { int n1; Term(out n); while (la.kind == 3) { Get(); Term(out n1); n = n + n1; } } void Term (out int n) { Expect(1); n = Convert.ToInt32(t.val); } ... }
1 ... number 2 ... "calc" 3 ... '+'
**>coco Sample.atg
csc Compile.cs Scanner.cs Parser.cs Compile Input.txt**
Sample (. int n; .) = { "calc" Expr
using System; using System.Collections;
int sum; void Add(int x) { sum = sum + x; }
digit = "0123456789". hexDigit = digit + "ABCDEF". letter = 'A' .. 'Z'. eol = '\r'. noDigit = ANY - digit.
the set of all digits the set of all hexadecimal digits the set of all upper-case letters the end-of-line character any character that is not a digit
\ backslash \r carriage return \f form feed ' apostrophe \n new line \a bell " quote \t horizontal tab \b backspace \0 null character \v vertical tab \uxxxx hex character value