Sample Translation-Compiler Building-Lecture Slides, Slides of Compiler Construction

This lecture was delivered by Ruiz Pereira at Chandra Shekhar Azad University of Agriculture

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dhansukh
dhansukh 🇮🇳

5

(2)

33 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
61
Sample Translation
Source code
void Foo() {
int a, b, max;
read a; read b;
if (a > b) max = a; else max = b;
write max;
}
Object code
1: ENTER 3
4: READ
5: STO 0
8: READ
9: STO 1
12: LOAD 0
15: LOAD 1
18: GTR
19: FJMP 31
22: LOAD 0
25: STO 2
28: JMP 37
31: LOAD 1
34: STO 2
37: LOAD 2
40: WRITE
41: LEAVE
42: RET
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Sample Translation-Compiler Building-Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Sample Translation

Source code

void Foo() { int a, b, max; read a; read b; if (a > b) max = a; else max = b; write max; }

Object code

1: ENTER 3

4: READ

5: STO 0

8: READ

9: STO 1

12: LOAD 0

15: LOAD 1

18: GTR

19: FJMP 31

22: LOAD 0

25: STO 2

28: JMP 37

31: LOAD 1

34: STO 2

37: LOAD 2

40: WRITE

41: LEAVE

42: RET

Scanner Specification

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.

Code Generator Class

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) {...} ... }

Parser Specification -- Declarations

PRODUCTIONS

Taste (. string name; .) = "program" Ident (. tab.OpenScope(); .) '{' { VarDecl } { ProcDecl } '}' (. tab.CloseScope(); .).

VarDecl (. string name; int type; .) = Type Ident (. tab.Insert(name, VAR, type); .) { ',' Ident (. tab.Insert(name, VAR, type); .) } ';'.

ProcDecl (. string name; Obj obj; int adr; .) = "void" Ident (. obj = tab.Insert(name, PROC, UNDEF); obj.adr = gen.pc; if (name == "Main") gen.progStart = gen.pc; tab.OpenScope(); .) '(' ')' '{' (. gen.Emit(ENTER, 0); adr = gen.pc - 2; .) { VarDecl | Stat } '}' (. gen.Emit(LEAVE); gen.Emit(RET); gen.Patch(adr, tab.topScope.adr); tab.CloseScope(); .).

Type = (. type = UNDEF; .) ( "int" (. type = INT; .) | "bool" (. type = BOOL; .) ).

public SymbolTable tab; public CodeGenerator gen;

Parser Specification -- Factor

Factor (. int n; Obj obj; string name; .) = (. type = UNDEF; .) ( Ident (. obj = tab.Find(name); type = obj.type; if (obj.kind == VAR) { if (obj.level == 0) gen.Emit(LOADG, obj.adr); else gen.Emit(LOAD, obj.adr); } else SemErr("variable expected"); .)

| number (. n = Convert.ToInt32(t.val); gen.Emit(CONST, n); type = INT; .)

| '-' Factor (. if (type != INT) { SemErr("integer type expected"); type = INT; } gen.Emit(NEG); .)

| "true" (. gen.Emit(CONST, 1); type = BOOL; .) | "false" (. gen.Emit(CONST, 0); type = BOOL; .) ).

Ident = ident (. name = t.val; .).

Parser Specification -- Statements

Stat (. int type; string name; Obj obj; int adr, adr2, loopstart; .) = Ident (. obj = tab.Find(name); .) ( '=' (. if (obj.kind != VAR) SemErr("can only assign to variables"); .) Expr ';' (. if (type != obj.type) SemErr("incompatible types"); if (obj.level == 0) gen.Emit(STOG, obj.adr); else gen.Emit(STO, obj.adr); .) | '(' ')' ';' (. if (obj.kind != PROC) SemErr("object is not a procedure"); gen.Emit(CALL, obj.adr); .) )

| "read" Ident ';' (. obj = tab.Find(name); if (obj.type != INT) SemErr("integer type expected"); gen.Emit(READ); if (obj.level == 0) gen.Emit(STOG, obj.adr); else gen.Emit(STO, obj.adr); .)

| "write" Expr ';' (. if (type != INT) SemErr("integer type expected"); gen.Emit(WRITE); .)

| '{' { Stat | VarDecl } '}'

Main Program of Taste

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"); }

Building the whole thing

c:> coco Taste.atg c:> csc Taste.cs Scanner.cs Parser.cs SymbolTable.cs CodeGenerator.cs c:> Taste Sample.tas

Summary

  • Coco/R generates a scanner and a recursive descent parser from an attributed grammar
  • LL(1) conflicts can be handled with resolvers

Grammars for C# and Java are available in Coco/R format

  • Coco/R is open source software (Gnu GPL)

http://ssw.jku.at/Coco/

  • Coco/R has been used by us to build
    • a white-box test tool for C#
    • a profiler for C#
    • a static program analyzer for C#
    • a metrics tool for Java
    • compilers for domain-specific languages
    • a log file analyzer
    • ...
  • Many companies and projects use Coco/R
    • SharpDevelop: a C# IDE www.icsharpcode.net
    • Software Tomography: Static Analysis Tool www.software-tomography.com
    • CSharp2Html: HTML viewer for C# sources www.charp2html.net
    • currently 39000 hits for Coco/R in Google