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

Lex Prog

This document is a lexical analyzer written in C using Flex, which defines patterns for recognizing various tokens such as keywords, operators, parentheses, braces, semicolons, commas, identifiers, and numbers. It includes rules for ignoring whitespace and comments, and it returns specific token types for recognized patterns. The main function calls the parser to process the input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Lex Prog

This document is a lexical analyzer written in C using Flex, which defines patterns for recognizing various tokens such as keywords, operators, parentheses, braces, semicolons, commas, identifiers, and numbers. It includes rules for ignoring whitespace and comments, and it returns specific token types for recognized patterns. The main function calls the parser to process the input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

%{

#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h" // Include the header file generated by Yacc
%}

%%

/* Whitespace and comments */


[ \t\r\n]+ { /* Ignore whitespace */ }
"//".* { /* Ignore comments */ }

/* Keywords */
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"int" { return INT; }
"float" { return FLOAT; }
"return" { return RETURN; }

/* Operators */
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MUL; }
"/" { return DIV; }
"=" { return ASSIGN; }
"==" { return EQ; }
"<" { return LT; }
">" { return GT; }
"!=" { return NEQ; }

/* Parentheses */
"(" { return LPAREN; }
")" { return RPAREN; }

/* Braces */
"{" { return LBRACE; }
"}" { return RBRACE; }

/* Semicolon */
";" { return SEMICOLON; }

/* Comma */
"," { return COMMA; }

/* Identifiers and numbers */


[a-zA-Z_][a-zA-Z0-9_]* { return ID; }
[0-9]+ { return NUM; }
[0-9]+\.[0-9]+ { return FLOATNUM; } // For floating-point numbers

%%

int yywrap() {
return 1;
}

int main() {
yyparse(); // Call the parser
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy