Skip to content

Instantly share code, notes, and snippets.

@RaneWallin
Last active November 1, 2020 01:15
Show Gist options
  • Save RaneWallin/c7c38b54b38af1754adf04dfc5755177 to your computer and use it in GitHub Desktop.
Save RaneWallin/c7c38b54b38af1754adf04dfc5755177 to your computer and use it in GitHub Desktop.
Example of a lex file
%option noyywrap
A [a-zA-Z]
DIGIT [0-9]
/* 1. include other pattern definitions here, if needed. Digits maybe? */
%{
#include "tokens.h" /* Leave this section untouched */
%}
%%
#.+
def return (DEF);
if return (IF);
then return (THEN);
while return (WHILE);
string return (STRING);
real return (REAL);
integer return (INTEGER);
{A}+ return (ID);
\".+\" return (STRING_CONST);
{DIGIT}+"."{DIGIT}* return (REAL_CONST);
{DIGIT}+ return (INT_CONST);
":=" return (ASSIGN);
">=" return (GREATER_EQUAL);
"<=" return (LESS_EQUAL);
"!=" return (NOT_EQUAL);
"+" return (PLUS);
";" return (SEMI);
":" return (COLON);
"," return (COMMA);
"(" return (LEFT_PAREN);
")" return (RIGHT_PAREN);
"[" return (LEFT_SQUARE);
"]" return (RIGHT_SQUARE);
"{" return (LEFT_BRACE);
"}" return (RIGHT_BRACE);
"^" return (CARAT);
"<" return (LESS_THAN);
">" return (GREATER_THAN);
"%" return (MOD);
"=" return (EQUAL);
"-" return (MINUS);
"*" return (MULT);
"/" return (DIVIDE);
"{"[^}\n]*"}"
[ \t\n]+
%%
void yyerror () /* Leave this section untouched */
{
printf (" error\n");
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment