// This is for UCSD CSE 141L, Spring 2006. -Wei and JSampson // // documentation on flex found in: // // http://jflex.de // // This is a source file for Java flex. Flex compiles this file // into a text processing tool using the grammar specified below. // Run flex as to generate Sample_flex.java: // > jflex sample_flex.flex // // The makefile adds an option to rename the generated output file. // // The generated Sample_flex.java will provide an API to recognize // certain strings. See further comments below import java.util.*; %% %{ // The following definitions specifies regular expressions. Regular // expressions are patterns that may match a given string, indicating // some action is to be taken. The action may communicate things like // what is it classified as, along with other information. // Everything between %{ and %} are directly passed to Sample_flex.java public String token; public String getToken() { return token; } // The following five lines tells jflex to name the new class Sample_flex, // keep track of line numbers, column count, // use C/C++ flex style yylex interface that returns an integer, andn // assume unicode strings. %} %class Sample_flex %line %column %byaccj %unicode ws=[ \t]+ token=[a-zA-Z][a-zA-Z0-9_\-]* comment=\#[^\n]+ %% \n { /* This section contains several "rules" consisting of an * a "pattern" and "action". If an input string matches * the pattern, then the action executes, often passing * this information externally. In this actions you can see that * it return a macro defined NEWLINE (see Token_type.java). This is * used in Jparser.java to count line numbers. * The matched string is visible externally as yytext() method. */ return Token_type.NEWLINE; } {ws} {} {comment} {} {token}: { token = yytext().substring(0,yytext().length()-1); return Token_type.LABEL; } {token} { token = yytext(); return Token_type.TOKEN; }