0% found this document useful (0 votes)
51 views3 pages

Accessor - and - Array

Uploaded by

api-744667975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Accessor - and - Array

Uploaded by

api-744667975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

Hunter Brazee
CS-210 Assignment 4
This program uses lex and built a scanner that satisfies the same requirements from homework 1
To use another file as input, it must be typed into the command line
*/
%{
/*
* This is part of the definitions section. It starts with %{ and ends with %}.
* Any text placed in this area will be copied verbatim into the lex.yy.c
* output file that lex generates, and will be placed near the top of that file.
*
* Typically this section is used to #include header files that will be needed
* in the generated scanner, but any legal C code can be placed here.
* For the assignment, no extra header files are needed, so this section is empty
*/
%}
/*
* This is also part of the definitions section. Anything after the %} above
* until the %% below is interpreted by lex as formal definitions. Things
* here won't be copied verbatim into the generated lex.yy.c output file,
* but will be analyzed by lex.
* The definitions for identifier, numeric literal, comments, strings, and character literal are defined
* Comments are indented to not start a new formal defintion in the leftmost column
*/
//Definiton for identifier, matches the first letter, which is upper or lower case, then followed by letter, digit
or underscore
ID [a-zA-Z][_a-zA-Z0-9]*
//Definiton for numeric literal, starts with digit 0-9, followed by another digit, a hexidecimal digit, or the special
characters _ . and #
NUMLIT [0-9][0-9a-fA-F_.#]*
//Definiton for comments, which start with /*, then matches the contents of the comment, then matches the */
for the end of the comment, including the start and end symbols
COMMENT \/\*[^*]*\*+([^*/][^*]*\*+)*\/
//Definiton for string,which starts with the first ", then matches the rest of the string as the lexeme, including
the " "
STRING \"[^\"]*\"
//Definiton for character literal, which starts with ', followed by the character, then the closing '
CHARLIT \'[a-zA-Z]*\'
%%
/*
* This section defines the rules which are analyzed by lex, and each has a pattern and a rule
* Patterns for keyword and operator are chained using | which allows for the chained to match those lexemes
* This section defines all of the patterns and rules for printing the specific lexemes
* Comments are indented in this secton because leftmost is the start of new pattern rule
*/
/* Rules for num literal, comment, string and char literal are analyzed*/
{NUMLIT} { printf("%s (numeric literal)\n", yytext); }
{COMMENT} { printf("%s (comment)\n", yytext); }
{STRING} { printf("%s (string)\n", yytext); }
{CHARLIT} { printf("%s (character literal)\n", yytext); }
/* List of specifc keywords to catch as keyword lexeme before identifier */
accessor |
and |
array |
bool |
case |
character |
constant |
else |
elsif |
end |
exit |
float |
func |
if |
ifc |
in |
integer |
is |
mutator |
natural |
null |
of |
or |
others |
out |
pkg |
positive |
proc |
ptr |
range |
subtype |
then |
type |
when |
while {
/* The action to be taken when one of the specified keywords is found */
printf("%s (keyword)\n", yytext );
}
/* Rule for identifier */
{ID} printf("%s (identifier)\n", yytext);
/*List of symbols that are operator lexemes */
"."|"<"|">"|"("|")"|"+"|"-"|"*"|"/"|"|"|"&"|";"|","|":"|"="|":="|".."|"<<"|">>"|"<>"|"<="|">="|"**"|"!="|"=>"|"["|"]"|"{"|"}"|"{:
"|"}:"|"$"|"@" printf("%s (operator)\n", yytext);
[ \t\n]+ /* discard whitespace */
/* Prints UNK for unknown lexeme */
. printf("%s (UNK)\n", yytext);
%%
/*
* This section is the user subroutines section. It starts after the %% above
* and continues to the end of the file. Anything appearing in the section is
* copied verbatim into the lex.yy.c file generated by lex, and will appear
* toward the bottom of that file.
*
* This section is where subroutines specific to a given scanner are placed.
* Any legal C code can be placed here.
*
*
* A main function is defined which guides the lexer, and takes the file name on the command line as the input
* Not entering a file on the command line will allow the user to type in thier own input,
* however this is only used for testing and not using the file as input, that must be done on the command line
*/
int main(int argc, char **argv)
{
if (argc > 1)
yyin = fopen(argv[1], "r"); //Uses the file name on the command line as input
else
yyin = stdin; //Used for testing the lexemes for output
yylex();
}
/*
* This defines yywrap, a special function used by lex to determine what should
* be done when the end of an input file is encountered.
*/
int yywrap()
{
return 1;
}

You might also like