






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 / 11
This page cannot be seen from the preview
Don't miss anything!
void Foo() { int a, b, max; read a; read b; if (a > b) max = a; else max = b; write max; }
COMPILER Taste
CHARACTERS letter = 'A'..'Z' + 'a'..'z'. digit = '0'..'9'.
TOKENS ident = letter {letter | digit}. number = digit {digit}.
COMMENTS FROM "/" TO "/" NESTED COMMENTS FROM "//" TO '\r' '\n'
IGNORE '\r' + '\n' + '\t'
PRODUCTIONS ... END Taste.
public class CodeGenerator { public int pc ; public int progStart ; public CodeGenerator () {...} public void Emit (int op) {...} public void Emit (int op, int val) {...} public void Patch (int adr, int val) {...} ... }
Taste (. string name; .) = "program" Ident
VarDecl (. string name; int type; .) = Type
ProcDecl (. string name; Obj obj; int adr; .) = "void" Ident
Type
public SymbolTable tab; public CodeGenerator gen;
Factor
| number (. n = Convert.ToInt32(t.val); gen.Emit(CONST, n); type = INT; .)
| '-' Factor
| "true" (. gen.Emit(CONST, 1); type = BOOL; .) | "false" (. gen.Emit(CONST, 0); type = BOOL; .) ).
Ident
Stat (. int type; string name; Obj obj; int adr, adr2, loopstart; .) = Ident
| "read" Ident
| "write" Expr
| '{' { Stat | VarDecl } '}'
using System;
public class Taste {
public static void Main (string[] arg) { if (arg.Length > 0) { Scanner scanner = new Scanner(arg[0]); Parser parser = new Parser(scanner); parser.tab = new SymbolTable(parser); parser.gen = new CodeGenerator(); parser.Parse(); if (parser.errors.count == 0) parser.gen.Interpret("Taste.IN"); } else Console.WriteLine("-- No source file specified"); }
c:> coco Taste.atg c:> csc Taste.cs Scanner.cs Parser.cs SymbolTable.cs CodeGenerator.cs c:> Taste Sample.tas