Chris Pollett > Students >
Yan Yao

    ( Print View )

    [Bio]

    [CS297Proposal]

    [Del1]

    [Del2]

    [Del3]

    [Del4]

    [CS297Report-PDF]

    [CS298Proposal]

    [CS298Presentation-PDF]

    [CS298Report-PDF]

    [Grad Photo-JPG]

                                

























Del 4: Polynomial Keyword Recognizer

Description: The main task of deliverable #4 was to write a translator using lex and yacc. The translator can parse a valid C++ file and can recognize the polynomial keyword in this file.

1. The Polynomial Tag

A polynomial tag `polynomial' was introduced into my project. The idea of the tag is that if a polynomial tag is put in front a function, we shall apply our restrictions to this function to see if it is a polynomial function. The polynomial tag can be placed at any part of the file. However, it must be placed in front of valid function. So it is very important to recognize a valid polynomial tag. In deliverable #4, due to the lack of time I only developed a translator to recognize the polynomial key word in a C++ source file.

2. Implementation

The implementation of this translator can be divided into two parts: lexer part and parser part. The lexer reads the source file and returns the tokens to the parser; the parser has some rules to check if this is a valid polynomial function.

2.1 Tokens

In a C++ source file, code can be recognized as several main groups.

a) Preprocessors: if an input file has a `#' at the beginning of the file, we ignore the whole line;

b) Data/function type: int, float, char, double, void, bool ;

c) Special symbols: `(', `)', `{', `}', `;' ;

d) Polynomial tag: polynomial;

e) Function/variable name: any text except the keyword we stated above.

Those key words will be return to the parser as tokens. I have not defined the tokens that appear as function parameters and contents inside the function body. I simplified this step by applying any texts inside the ( ) or {} as valid contents.

2.2 Rules

The parser takes the tokens and feeds them into the rules. In this simple translator, I gave some rules to identify basic structure of a C++ source file and the polynomial function. A source file mainly consists of five elements: preprocessors, declaration, function, function prototype and polynomial function. Please note that this is just a simplified definition of C++ source file.

a) Preprocessor: if a `#' is met, we consider this is a preprocessor;

b) Declaration: a declaration consists of a data type, variable name and a semicolon. It's format should be like this:

Data type variable name;

c) Function prototype: a function prototype's format :

 
Function type function name ( anything );

d) Function: a function consists of a function type, function name, parameters and function body.

Function  function name ( anything)
{

anything

}

e) Polynomial function: a polynomial function is a polynomial tag followed by a valid function:

Polynomial Function type function name ( anything)
{

anything

}

Lex Code

%{

#include <stdio.h>

#include <string.h>


#include "del4.tab.h"

%}



%%

[\t\n];

^#.*		{printf("%s\n", yytext);}

polynomial 	{printf("%s", yytext);return TAG;}
 

int   		{printf("%s", yytext);return INT;} 


void 		{printf("%s", yytext);return VOID;}

double		{printf("%s", yytext);return DOUBLE;}

bool		{printf("%s", yytext);return BOOL;}

char 		{printf("%s", yytext);return CHAR;}

float		{printf("%s", yytext);return FLOAT;}

[a-zA-Z]*	 {printf("%s", yytext);return CONTENT;}

\(	|
\)	|
\{	|
\}	|
\;   {printf("%s\n", yytext); return yytext[0];}

 

%%

int yywrap(void)

 {

    return 1;

}



int main(int argc, char *argv[])
 
{
   
 yyin = fopen(argv[1], "r");
 
 
    yyparse();
  
return 0;  

}


Yacc Code

%{

#include <stdio.h>

#include <string.h>


%}







%token 	TAG INT VOID DOUBLE BOOL CHAR FLOAT CONTENT 



%%
start : start poly {printf("parsed a poly function\n");} 
  |start declar {printf("parsed a dec\n");} |
  start func_prot {printf("parsed a func prot\n");} 
  | start func {printf("parsed a function\n");}|;

poly: TAG  func {};

func: func_dec func_body;

declar: func_head ';' ;

func_prot: func_dec ';' ;

func_dec: func_head func_para; /*such as int abb (a)*/

func_head: func_type name ; /*such as int abb */

func_para: '(' quoted_content')' {};

func_body: '{' quoted_content '}' {};

quoted_content: quoted_content CONTENT | ; /* content in () or {}*/

name: CONTENT; /*fuc name or variable name*/

func_type: INT | VOID |DOUBLE |BOOL| CHAR |FLOAT ;

 
%%
yyerror(s)

char *s;

{printf("%s", s);}