Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Flex (lexical analyser generator)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example lexical analyzer == This is an example of a Flex scanner for the instructional programming language [[PL/0]]. The tokens recognized are: '<code>+</code>', '<code>-</code>', '<code>*</code>', '<code>/</code>', '<code>=</code>', '<code>(</code>', '<code>)</code>', '<code>,</code>', '<code>;</code>', '<code>.</code>', '<code>:=</code>', '<code><</code>', '<code><=</code>', '<code><></code>', '<code>></code>', '<code>>=</code>'; numbers: <code>0-9 {0-9}</code>; identifiers: <code>a-zA-Z {a-zA-Z0-9}</code> and keywords: <code>begin</code>, <code>call</code>, <code>const</code>, <code>do</code>, <code>end</code>, <code>if</code>, <code>odd</code>, <code>procedure</code>, <code>then</code>, <code>var</code>, <code>while</code>. <syntaxhighlight lang="c"> %{ #include "y.tab.h" %} digit [0-9] letter [a-zA-Z] %% "+" { return PLUS; } "-" { return MINUS; } "*" { return TIMES; } "/" { return SLASH; } "(" { return LPAREN; } ")" { return RPAREN; } ";" { return SEMICOLON; } "," { return COMMA; } "." { return PERIOD; } ":=" { return BECOMES; } "=" { return EQL; } "<>" { return NEQ; } "<" { return LSS; } ">" { return GTR; } "<=" { return LEQ; } ">=" { return GEQ; } "begin" { return BEGINSYM; } "call" { return CALLSYM; } "const" { return CONSTSYM; } "do" { return DOSYM; } "end" { return ENDSYM; } "if" { return IFSYM; } "odd" { return ODDSYM; } "procedure" { return PROCSYM; } "then" { return THENSYM; } "var" { return VARSYM; } "while" { return WHILESYM; } {letter}({letter}|{digit})* { yylval.id = strdup(yytext); return IDENT; } {digit}+ { yylval.num = atoi(yytext); return NUMBER; } [ \t\n\r] /* skip whitespace */ . { printf("Unknown character [%c]\n",yytext[0]); return UNKNOWN; } %% int yywrap(void){return 1;} </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)