CSci 169 - Implementing Static Type Checker for C-- - Prof. Abdelghani Bellaachia, Assignments of Computer Science

A programming assignment for a university course, csci 169 – software paradigms, offered at the george washington university's school of engineering and applied science, department of computer science. Students are tasked with implementing a static type checker, tcc, for the c-- programming language. The ebnf grammar for c-- and explains the requirements for the type checker, including error handling and detection of type inconsistencies and incompatibilities.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-f6a
koofers-user-f6a 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The George Washington University
School of Engineering and Applied Science
Department of Computer Science
CSci 169 – Software Paradigms – Fall 2007
Programming Assignment # 3: A Static Type Checker
Due Date: Wednesday, November 14, 2007
Instructor: A. Bellaachia
Send the electronic version of your assignment to: [email protected]
The following represents the EBNF grammar for a very simple language, called C--:
program ----> {stmt}
stmt ----> var_dec ";" | assign ";" | read_stat ";" |
write_stat ";" | if_stmt ";" | while_stmt ";"
var_dec ----> type var
assign ----> var "=" expr
expr ----> add_expr
add_expr ----> mul_expr {("+"|"-") mul_expr}
mul_expr ----> simple_expr {("*"|"/"|"%") simple_expr }
simple_expr ----> id | var | "(" expr ")"
read_stat ----> "READ" "(" expr ")"
write_stat ----> "PRINT" "(" expr ")"
type ----> "int" | "float" | "boolean"
id ----> Digit | Digit id
Digit ----> [0-9]+
boolean ----> "0" | "1";
var ----> [A-Z, a-z]+
block ----> program [ block ]
if_stmt ----> "if" bool_stmt ":" [block] [ "else:" [block] ] "end if"
while_stmt ----> "while" bool_stmt "do" [block] "end while"
bool_stmt ----> and_stmt | rel_stmt |boolean
and_stmt ----> bool_stmt {("and"|"or") bool_stmt}
rel_stmt ----> simple_expr (">"|"<"|">="|"==") simple_expr
In this project, you would like to implement, in C++, a strict static type checker, we call
TCC for C--. Remember, you only need to check the types of all statements. TCC
reads a C--program and should:
You can use the lexical analyzer you developed in assignment 2.
Print “Your program is type error free”, if there are no type errors in the program,
or Print a brief, accurate description (including line numbers) of each error, if
there are any type errors in the program.
pf3
pf4

Partial preview of the text

Download CSci 169 - Implementing Static Type Checker for C-- - Prof. Abdelghani Bellaachia and more Assignments Computer Science in PDF only on Docsity!

The George Washington University School of Engineering and Applied Science Department of Computer Science CSci 169 – Software Paradigms – Fall 2007 Programming Assignment # 3: A Static Type Checker Due Date: Wednesday, November 14, 2007 Instructor: A. Bellaachia

Send the electronic version of your assignment to: [email protected]

The following represents the EBNF grammar for a very simple language, called C--:

program ----> {stmt} stmt ----> var_dec ";" | assign ";" | read_stat ";" | write_stat ";" | if_stmt ";" | while_stmt ";" var_dec ----> type var assign ----> var "=" expr expr ----> add_expr add_expr ----> mul_expr {("+"|"-") mul_expr} mul_expr ----> simple_expr {("*"|"/"|"%") simple_expr } simple_expr ----> id | var | "(" expr ")" read_stat ----> "READ" "(" expr ")" write_stat ----> "PRINT" "(" expr ")" type ----> "int" | "float" | "boolean" id ----> Digit | Digit id Digit ----> [0-9]+ boolean ----> "0" | "1"; var ----> [A-Z, a-z]+ block ----> program [ block ] if_stmt ----> "if" bool_stmt ":" [block] [ "else:" [block] ] "end if" while_stmt ----> "while" bool_stmt "do" [block] "end while" bool_stmt ----> and_stmt | rel_stmt |boolean and_stmt ----> bool_stmt {("and"|"or") bool_stmt} rel_stmt ----> simple_expr (">"|"<"|">="|"==") simple_expr

In this project, you would like to implement, in C++, a strict static type checker, we call TCC for C--. Remember, you only need to check the types of all statements. TCC reads a C--program and should:

  • You can use the lexical analyzer you developed in assignment 2.
  • Print “Your program is type error free”, if there are no type errors in the program, or Print a brief, accurate description (including line numbers) of each error, if there are any type errors in the program.
  • Your type checker should statically detect all of the following type errors: o Incorrect or inconsistent type of expressions (for example, using a floating-point value as the loop condition in a while loop). o Incompatible types used within expressions (for example, adding an integer and a floating-point value). o Legal operations for each type, e.g., cannot multiply character type.
  • Sample input data:

{ Int x ; Int y ; Int z ; X = 10 ; Y = 20 ; print (x); print (x+y); read(z) z = z + x + y; print (z); }

{ if 5 > 1: { if (4>3) and (2 <5) : { print 1; print 2; } else: { print 3; print (3+1) } end if; } end if; }

  • Notes:

Call your lexical analyzer (); //to get the next token. else if (nextToken == LEFT_PAREN_CODE ) { Call your lexical analyzer (); expr(); if (nextToken == RIGHT_PAREN_CODE ) Call your lexical analyzer (); else errors(); } else errors(); /* The expression has an error. */