Docsity

C Language Subset: Syntax Analysis Project, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
Project 1: syntax analysis (30pts)
Due Oct 15, 2008 (corrections may be made until final)
In this project, you are required to build a scanner and a parser for a subset of the C
language (you may pick a different language other than C, but your language must support
an equivalent set of terminals/tokens and non-terminals/concepts).
Assuming that you decide to parse a subset of the Clanguage, your scanner need to
recognize the following token types.
โ€ขSeparators: SEMICOLON(โ€œ;โ€), LPAREN(โ€œ(โ€), RPAREN(โ€œ)โ€), COMMA(โ€œ,โ€), LBRACE(โ€œ{โ€),
RBRACE(โ€œ}โ€), LBRACKET(โ€œ[โ€) and RBRACKET(โ€œ]โ€).
โ€ขOperators: ASSIGN(โ€œ=โ€), LT(โ€œ<โ€), GT(โ€œ>โ€), EQ(โ€œ==โ€), PLUS(โ€œ+โ€), MINUS(โ€œ-
โ€), MULT(โ€œ*โ€), DIV(โ€œ/โ€), NOT(โ€œ!โ€), AND(โ€œ&&โ€), OR(โ€œ||โ€)
โ€ขKeywords: IF(โ€œifโ€), ELSE(โ€œelseโ€), WHILE(โ€œwhileโ€), INT(โ€œintโ€), FLOAT(โ€œfloatโ€).
โ€ขIdentifiers: ID, which includes all user-defined names in C.
โ€ขInteger constants: ICONST. Examples include 3, 235, etc.
โ€ขFloating point constants: FCONST. Examples include 3.2, 5E2, 2.3e-3, etc.
โ€ขComments: COMMENT. All strings enclosed within โ€œ/*โ€ and โ€œ*/โ€.
Based on the above defined tokens, your parser needs to support the following language
constructs.
โ€ขVariable declarations for integer, floating point values and arrays. e.g.
int x, y, z;
float a, b, c;
float arr[100], arr2[100][50];
โ€ขInteger and floating point expressions that support ASSIGN(โ€œ=โ€), LT(โ€œ<โ€), GT(โ€œ>โ€),
EQ(โ€œ==โ€), PLUS(โ€œ+โ€), MINUS(โ€œ-โ€), MULT(โ€œ*โ€), DIV(โ€œ/โ€), NOT(โ€œ!โ€), AND(โ€œ&&โ€),
OR(โ€œ||โ€). e.g.
!(a==b+1*(c-1)/2)
โ€ขExpression statements. For example, โ€œa = b+c; โ€ and โ€œa+b;โ€ are both expression
statements, each of which contains a single expression.
โ€ขBlock statements (sequences of statements enclosed within pairs of braces), e.g.
1
pf2

Partial preview of the text

Download C Language Subset: Syntax Analysis Project and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Project 1: syntax analysis (30pts)

Due Oct 15, 2008 (corrections may be made until final)

In this project, you are required to build a scanner and a parser for a subset of the C language (you may pick a different language other than C, but your language must support an equivalent set of terminals/tokens and non-terminals/concepts). Assuming that you decide to parse a subset of the C language, your scanner need to recognize the following token types.

  • Separators: SEMICOLON(โ€œ;โ€), LPAREN(โ€œ(โ€), RPAREN(โ€œ)โ€), COMMA(โ€œ,โ€), LBRACE(โ€œ{โ€), RBRACE(โ€œ}โ€), LBRACKET(โ€œ[โ€) and RBRACKET(โ€œ]โ€).
  • Operators: ASSIGN(โ€œ=โ€), LT(โ€œ<โ€), GT(โ€œ>โ€), EQ(โ€œ==โ€), PLUS(โ€œ+โ€), MINUS(โ€œ- โ€), MULT(โ€œ*โ€), DIV(โ€œ/โ€), NOT(โ€œ!โ€), AND(โ€œ&&โ€), OR(โ€œ||โ€)
  • Keywords: IF(โ€œifโ€), ELSE(โ€œelseโ€), WHILE(โ€œwhileโ€), INT(โ€œintโ€), FLOAT(โ€œfloatโ€).
  • Identifiers: ID, which includes all user-defined names in C.
  • Integer constants: ICONST. Examples include 3, 235, etc.
  • Floating point constants: FCONST. Examples include 3.2, 5E2, 2.3e-3, etc.
  • Comments: COMMENT. All strings enclosed within โ€œ/โ€ and โ€œ/โ€.

Based on the above defined tokens, your parser needs to support the following language constructs.

  • Variable declarations for integer, floating point values and arrays. e.g.

int x, y, z; float a, b, c; float arr[100], arr2[100][50];

  • Integer and floating point expressions that support ASSIGN(โ€œ=โ€), LT(โ€œ<โ€), GT(โ€œ>โ€), EQ(โ€œ==โ€), PLUS(โ€œ+โ€), MINUS(โ€œ-โ€), MULT(โ€œ*โ€), DIV(โ€œ/โ€), NOT(โ€œ!โ€), AND(โ€œ&&โ€), OR(โ€œ||โ€). e.g.

!(a == b + 1 * (c - 1) / 2)

  • Expression statements. For example, โ€œa = b+c; โ€ and โ€œa+b;โ€ are both expression statements, each of which contains a single expression.
  • Block statements (sequences of statements enclosed within pairs of braces), e.g.

{a = b + c; d = a * 2; }

  • If-else statements, e.g., โ€œif (a < b) c = b; else c = a; โ€.
  • While statements, e.g., โ€œwhile (a < b) a = a + 1; โ€.

The follow is a sample input to be processed by your scanner and parser.

float a[100][100], b[100][100], c[100][100]; int i, j, k; i = 0; while (i < 100) { j = 0; while (j < 100) { if (!(c[i][j] == 0)) c[i][j] = 0; k = 0; while (k < 100) { c[i][j] = c[i][j] + a[i][k] * b[k][j]; k = k + 1; } j = j + 1; } i = i + 1; }

You may choose to write the scanner/parser manually using C/C++/Java, or use au- tomatic scanner/parser generators such as the lex and yacc automatic scanner/parser gen- erators under Unix/Linux. You can also the POET transformation scripting language to implement your scanner and parser. Using POET is encouraged as it is a research language under active development. You can find help on lex, yacc at the class web site or simply typing โ€œman lexโ€ and โ€œman yaccโ€ on your Linux or Unix machine. The POET software as well as its language manual can also be downloaded from the class web site. Submit your project by packaging up your directory into a gzip file and then sending the file via email to [email protected] โ€” NOTE: DO NOT submit to [email protected], as this will email your project to everyone of the entire class!!!. Suppose your entire project is in a directory named โ€œproject1โ€. Goto the parent directory of project1 and type

tar -cf project1.tar project gzip project1.tar

A file project1.tar.gz will be generated. Make sure that you make it very clear which approach you have chosen to implement the project, especially if you are using an approach different from any of the options listed in this assignment. If you used any tool beyond those mentioned in this assignment, you also need to make sure that we have access to the same tool (letting us know how to download it). And if you have chosen a language different than C, you must provide your own test file which is equivalent to the one given.