0% found this document useful (0 votes)
31 views2 pages

Include 1

This document contains a C implementation of a lexical analyzer (lexer) that processes input text to identify tokens such as operators, parentheses, and identifiers. It uses a static input buffer to read lines of text, skipping whitespace and handling illegal characters. The lexer maintains state information such as the current lexeme, its length, and the line number for error reporting.

Uploaded by

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

Include 1

This document contains a C implementation of a lexical analyzer (lexer) that processes input text to identify tokens such as operators, parentheses, and identifiers. It uses a static input buffer to read lines of text, skipping whitespace and handling illegal characters. The lexer maintains state information such as the current lexeme, its length, and the line number for error reporting.

Uploaded by

Alvin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include "lex.

h"
#include <stdio.h>
#include <ctype.h>

char *yytext = ""; /* Lexeme (not '\0' terminated)*/


int yyleng = 0; /* Lexeme lenght */
int yylinenode = 0; /* Input line number */

int lex()
{
static char input_buffer[128];
char *current;

current = yytext + yyleng; /* Skip current lexeme */

while(1) /* Get the next one */


{
while(!*current)
{
/* Get new lines, skipping any leading white space on the line,
* until a nonblank line is found */

current = input_buffer;
if(!gets (input_buffer))
{
*current = '\0';
return EOI;
}
++yylineno;

while(isspace (*current))
++current;
}

for(; *current ; ++current)


{
/* Get the next token */

yytext = current;
yyleng = 1;

switch(*current)
{
case EOF : return EOF;
case ';' : return SEMI;
case '+' : return PLUS;
case '*' : return TIMES;
case '(' : return LP;
case ')' : return RP;

case '\n':
case '\t':
case ' ': break;

default:
if(!isalnum(*current))
fprintf(stderr, "Ignoring illegal input <%c>\n",
*current);
else
{
while(isalnum(*current))
++current;

yyleng = current - yytext;


return NUM_OR_ID;
}

break;
}
}
}
}

static int Lookahead = -1; /* Lookahead token*/

int match (token)


int token;
{
/* Return true if "token" matches the current lookahead symbol. */

if( Lookahead == -1)


Lookahead = lex();

return token == Lookahead;


}
void advance()
{
/* Advance the lookahead to the nect symbol.*/
Lookahead = lex();
}

You might also like