CMPSC 461
PROGRAMMING LANGUAGES CONCPETS
PROJECT 1
SUMMER 2019
DUE JUNE 14, 2019 BY 11:59PM
Project:
Design a lexical analyzer for the fragment of the language shown below. Simulate the lexical analyzer using C++ language. Your scanner first should generate tokens (identifiers, operators, symbols, keywords, comments) and then when reading the input expressions, it should do the following:
- If the input is alphabet or digits then store it as identifier. An identifier is defines as a variable or a constant (digit(s)). You can limit the length of any identifier to 10 characters. For example in the code below: variables are: arr, t1, t2, t3, constants are: 1 2 3 10 5 199 -125
- If it is an invalid identifier (starting with a digit followed by a character for example) your code should print an error message that the identifier is not valid. Example: 1abc is not a valid identifier.
- Recognize the symbols and display an error message if the symbol is not recognized. Example: : , ; () {} are symbols in the code below.
- Recognize an operator and store it as an operator. If the given input matches with any operator symbol then display it in terms of words of the particular symbol. Else print not an operator. Example: * is a multiplication operator, !> is not an operator.
- If it input is a keyword then store it as a keyword. Example: keywords in the code below are: int if then else endif
- Your program should also recognizes comments. This include comments between // and /* */. Example:
//hello is a comment
/* I am here */ is a comment
Hello is not a comment.
Here is the fragment of a program that you should work on:
{
int arr[3],t1,t2;
int 1qbc;
t1=2;arr[0]=1;arr[1]=2;arr[2]=3;
t2=-(arr[2]+t1*10)/(arr[2]-t1);
if t2>5
then print(t2)
else
{
int t3;
t3=199;
t2=-125;
print(-t1+t2*t3);
/*this is a comment*/
}
endif
}
$
$ indicates the end of the program.
Sample run of the above code:
Variables or Identifiers: arr[0] arr[1] arr[2] arr[3] t1 t2 t3 print
1abc is not a valid identifier.
Operator: – + * / > =
Constants: 2 1 3 10 5 199 -25
Keywords: int if then else endif
Special Symbols: , ; ( ) { }
Comments: this is a comment
Total: as shown in Canvas
Submission:
Write a C++ file (with appropriate documentation). Upload your .cpp file on Canvas by the due date. Late projects are not accepted. No exceptions.