Docsity

Practice Final Exam - Programming Language Translators | COP 4620, Exams of Computer Science

Partial preview of the text

Download Practice Final Exam - Programming Language Translators | COP 4620 and more Exams Computer Science in PDF only on Docsity!

COP 4620/

PROGRAMMING LANGUAGE TRANSLATORS

Instructor: Manuel Bermudez

FALL 2007

FINAL EXAM

Take-home, Drop-dead deadline: 12 noon Wednesday Dec. 12

Turn in at my office or at E301 CSE

Problem 1 (15p.)

Problem 2 (15p.)

Problem 3 (30p.)

Problem 4 (20p.)

Problem 5 (20p.)

SCORE (100p.)

NAME

PROBLEM 1. (15 points)

Convert the following regular expression to a NFA, then transform the NFA to a

right-linear regular grammar, and finally transform the grammar to the original regular

expression. Show your work.

(ab* + c)* de* + f*

PROBLEM 2. (15 points)

Write a string-to-tree transduction grammar, with the following characteristics.

1. The language is composed of expressions, with operators and the atomic symbol ’a’.

2. The language has the following operator precedence hierarchy.

Level Operator Type of Op. Associativity

Least binding ’&’,’*’ Binary Infix Left

’ˆ’,’%’,’˜’ Binary Infix Right

’#’,’+’ Binary Infix Left

’|’ Unary Prefix None

’@’ Unary Postfix None

Most binding ’(’ ’)’ Embedding None

Show the derivation tree and the abstract syntax tree of the following expression:

a & a * | a # a ˆ | | a @ + a % a ˜ a

WORKSPACE

a) Show that the above gramamr is not LL(1).

b) Transform the grammar to make it LL(1), calculate Select sets and show the modi-

fied grammar is LL(1).

c) Show the parse table for the modified grammar.

d) Write a recursive descent parser (in pseudo-code) for this language, adding

’BuildTree’ statements to build the Abstract Syntax Tree for the original grammar.

e) Show a step-by-step parse for each of the two sample regular expressions shown

above. In each step, show the stack, the remaining input, and any BuildTree invoca-

tions.

WORKSPACE

Now, assume someone has already undertaken the task of developing a recursive

descent parser for this grammar.

c) Show the derivation tree and the abstract syntax tree for the following input:

DLVII, MI, MCMXCIV

d) Below is a (slightly incomplete) roman compiler back-end program, written in

pseudo code. The code for cases "Plus" and "Minus" is missing. YOU FILL

THEM IN.

program RomanBackEnd(input,output); const

One = ’1’; Five = ’5’; Ten = ’10’; Fifty = ’50’; Hundr = ’100’; { These are Node Names } FiveH = ’500’; Thous = ’1000’; Plus = ’+’; Minus = ’-’;

var i : integer; function Traverse (T:TreeNode): integer; var N,i: integer; begin

case NodeName(T) of

Plus: Minus: One : Traverse := 1; Five : Traverse := 5; Ten : Traverse := 10; Fifty : Traverse := 50; Hundr : Traverse := 100; FiveH : Traverse := 500; Thous : Traverse := 1000;

end

end; begin {main}

ReadTree;

for i := 1 to Rank(RootOfTree(1)) do

writeln(’Value Is ’,Traverse (Child(Root,i)));

end.

e) Annotate each "+" and "-" in your AST with the value returned by function

"Traverse", when applied to that node. Show the output of the roman compiler

back-end.

WORKSPACE

WORKSPACE

TINY’S GRAMMAR:

Tiny -> PROGRAM Name ’:’ Dclns Body Name ’.’ => "program";

Dclns -> VAR (Dcln ’;’)* => "dclns"

-> => "dclns";

Dcln -> Name list ’,’ ’:’ Type => "dcln";

Type -> INTEGER => "integer"

-> BOOLEAN => "boolean";

Body -> BEGINX Statement list ’;’ END => "block";

Statement -> Name ASSIGNMENT Expression => "assign"

-> OUTPUT ’(’ Expression ’)’ => "output"

-> IF Expression THEN Statement

ELSE Statement => "if"

-> WHILE Expression DO Statement => "while"

-> Body

-> => "";

Expression -> Term

-> Term LTE Term => "<=";

Term -> Primary

-> Primary ’+’ Term => "+";

Primary -> ’-’ Primary => "-"

-> READ => "read"

-> Name

-> INTEGER_NUM => ""

-> ’(’ Expression ’)’;

Name -> IDENTIFIER => "";

void InitializeConstrainer (int argc, char *argv[]) { int i, j;

InitializeTextModule(); InitializeTreeModule();

for (i=0, j=1; i<19; i++, j++) String_Array_To_String_Constant (node[i], j);

index = System_Flag ("-trace", argc, argv);

if (index) { TraceSpecified = true; TraceFileName = System_Argument ("-trace", "_TRACE", argc, argv); TraceFile = Open_File(TraceFileName, "w"); } else TraceSpecified = false; }

void AddIntrinsics (void) { TreeNode TempTree;

AddTree (TypesNode, RootOfTree(1), 2);

TempTree = Child (RootOfTree(1), 2); AddTree (TypeNode, TempTree, 1);

TempTree = Child (RootOfTree(1), 2); AddTree (TypeNode, TempTree, 1);

TempTree = Child (Child (RootOfTree(1), 2), 1); AddTree (BooleanTNode, TempTree, 1);

TempTree = Child (Child (RootOfTree(1), 2), 2); AddTree (IntegerTNode, TempTree, 1); }

void ErrorHeader (TreeNode T) { printf ("<<< CONSTRAINER ERROR >>> AT "); Write_String (stdout,SourceLocation(T)); printf (" : "); printf ("\n"); }

int NKids (TreeNode T) { return (Rank(T)); }

UserType Expression (TreeNode T) { UserType Type1, Type2; TreeNode Declaration, Temp1, Temp2; int NodeCount;

if (TraceSpecified) { fprintf (TraceFile, "<<< CONSTRAINER >>> : Expn Processor Node "); Write_String (TraceFile, NodeName(T)); fprintf (TraceFile, "\n"); }

switch (NodeName(T)) { case LENode : Type1 = Expression (Child(T,1)); Type2 = Expression (Child(T,2));

if (Type1 != Type2) { ErrorHeader(T); printf ("ARGUMENTS OF ’<=’ MUST BE TYPE INTEGER\n"); printf ("\n"); } return (TypeBoolean);

case PlusNode : case MinusNode : Type1 = Expression (Child(T,1));

if (Rank(T) == 2) Type2 = Expression (Child(T,2)); else Type2 = TypeInteger;

if (Type1 != TypeInteger || Type2 != TypeInteger) { ErrorHeader(T); printf ("ARGUMENTS OF ’+’, ’-’, ’*’, ’/’, mod "); printf ("MUST BE TYPE INTEGER\n"); printf ("\n"); } return (TypeInteger);

case ReadNode : return (TypeInteger);

case IntegerNode : return (TypeInteger);

case IdentifierNode : Declaration = Lookup (NodeName(Child(T,1)), T); if (Declaration != NullDeclaration) { Decorate (T, Declaration); return (Decoration(Declaration)); } else return (TypeInteger);

default : ErrorHeader(T); printf ( "UNKNOWN NODE NAME "); Write_String (stdout,NodeName(T)); printf ("\n");

} /* end switch / } / end Expression */

case OutputNode : for (Kid = 1; Kid <= NKids(T); Kid++) if (Expression (Child(T,Kid)) != TypeInteger) { ErrorHeader(T); printf ("OUTPUT EXPRESSION MUST BE TYPE INTEGER\n"); printf ("\n"); } break;

case IfNode : if (Expression (Child(T,1)) != TypeBoolean) { ErrorHeader(T); printf ("CONTROL EXPRESSION OF ’IF’ STMT"); printf (" IS NOT TYPE BOOLEAN\n"); printf ("\n"); }

ProcessNode (Child(T,2)); if (Rank(T) == 3) ProcessNode (Child(T,3)); break;

case WhileNode : if (Expression (Child(T,1)) != TypeBoolean) { ErrorHeader(T); printf ("WHILE EXPRESSION NOT OF TYPE BOOLEAN\n"); printf ("\n"); } ProcessNode (Child(T,2)); break;

case NullNode : break;

default : ErrorHeader(T); printf ("UNKNOWN NODE NAME "); Write_String (stdout,NodeName(T)); printf ("\n");

} /* end switch / } / end ProcessNode */

Copyright (C) 1986 by Manuel E. Bermudez Translated to C - 1991 ********************************************************************/

#include <stdio.h> #include <header/CommandLine.h> #include <header/Open_File.h> #include <header/Table.h> #include <header/Text.h> #include <header/Error.h> #include <header/String_Input.h> #include <header/Tree.h> #include <header/Code.h> #include <header/CodeGenerator.h> #define LeftMode 0 #define RightMode 1

/* ABSTRACT MACHINE OPERATIONS / #define NOP 1 / ’NOP’ / #define HALTOP 2 / ’HALT’ / #define LITOP 3 / ’LIT’ / #define LLVOP 4 / ’LLV’ / #define LGVOP 5 / ’LGV’ / #define SLVOP 6 / ’SLV’ / #define SGVOP 7 / ’SGV’ / #define LLAOP 8 / ’LLA’ / #define LGAOP 9 / ’LGA’ / #define UOPOP 10 / ’UOP’ / #define BOPOP 11 / ’BOP’ / #define POPOP 12 / ’POP’ / #define DUPOP 13 / ’DUP’ / #define SWAPOP 14 / ’SWAP’ / #define CALLOP 15 / ’CALL’ / #define RTNOP 16 / ’RTN’ / #define GOTOOP 17 / ’GOTO’ / #define CONDOP 18 / ’COND’ / #define CODEOP 19 / ’CODE’ / #define SOSOP 20 / ’SOS’ / #define LIMITOP 21 / ’LIMIT’ */

/* ABSTRACT MACHINE OPERANDS */

/* UNARY OPERANDS / #define UNOT 22 / ’UNOT’ / #define UNEG 23 / ’UNEG’ / #define USUCC 24 / ’USUCC’ / #define UPRED 25 / ’UPRED’ / / BINARY OPERANDS / #define BAND 26 / ’BAND’ / #define BOR 27 / ’BOR’ / #define BPLUS 28 / ’BPLUS’ / #define BMINUS 29 / ’BMINUS’ / #define BMULT 30 / ’BMULT’ / #define BDIV 31 / ’BDIV’ / #define BEXP 32 / ’BEXP’ / #define BMOD 33 / ’BMOD’ / #define BEQ 34 / ’BEQ’ / #define BNE 35 / ’BNE’ / #define BLE 36 / ’BLE’ / #define BGE 37 / ’BGE’ / #define BLT 38 / ’BLT’ / #define BGT 39 / ’BGT’ / / OS SERVICE CALL OPERANDS / #define TRACEX 40 / ’TRACEX’ / #define DUMPMEM 41 / ’DUMPMEM’ / #define OSINPUT 42 / ’INPUT’ / #define OSINPUTC 43 / ’INPUT’ / #define OSOUTPUT 44 / ’OUTPUT’ / #define OSOUTPUTC 45 / ’OUTPUT’ / #define OSOUTPUTL 46 / ’OUTPUTL’ / #define OSEOF 47 / ’EOF’ */

/* TREE NODE NAMES / #define ProgramNode 48 / ’program’ / #define TypesNode 49 / ’types’ / #define TypeNode 50 / ’type’ / #define DclnsNode 51 / ’dclns’ / #define DclnNode 52 / ’dcln’ / #define IntegerTNode 53 / ’integer’ / #define BooleanTNode 54 / ’boolean’ */