Transition table: complete, I think?

This commit is contained in:
Victor Fernandes 2017-03-14 15:54:47 -04:00
parent 668afe99c2
commit 42827b7f01
3 changed files with 118 additions and 64 deletions

View File

@ -10,9 +10,9 @@ L(KEYWORD) = PLATYPUS | IF | THEN | ELSE | USING | REPEAT | INPUT | OUTPUT
``` ```
## Variable Identifiers ## Variable Identifiers
``` ```
L(LETTER) = [a-zA-Z] L(L) = [a-zA-Z]
L(LETTER_OR_DIGIT) = [a-zA-Z0-9] L(LoD) = [a-zA-Z0-9]
L(VID) = AVID | SVID L(VID) = AVID | SVID
@ -24,24 +24,24 @@ L(SVID) = AVID#
## Integer Literals ## Integer Literals
``` ```
L(DEC_INT_LITERAL) = [0-9]* L(DIL) = 0 | [1-9]*
L(NON_ZERO_DIGIT) = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 L(NzD) = [1-9]
L(DIGIT) = [0-9]+ L(D) = [0-9]
L(OCT_DIGIT) = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 L(OD) = [0-7]
L(OCT_DIGIT_LITERAL) = 0(OCT_DIGIT)+ L(OIL) = 0([0-7])*
L(INT_LITERAL) = (DEC_INT_LITERAL | OCT_INT_LITERAL) L(IL) = (DIL | OIL)
``` ```
## Floating Point Literal ## Floating Point Literal
``` ```
L(FLP_LITERAL) = ([0-9]*).([0-9]+) L(FLPL) = (0 | [1-9]*).[0-9]*
``` ```
## String Literal ## String Literal
``` ```
L(STR_LITERAL) = "([a-ZA-Z_0-9])*" L(STRL) = "([a-ZA-Z_0-9])*"
``` ```

Binary file not shown.

122
table.h
View File

@ -21,11 +21,11 @@
#include <_null.h> /* NULL pointer constant is defined there */ #include <_null.h> /* NULL pointer constant is defined there */
#endif #endif
/* Source end-of-file (SEOF) sentinel symbol /* Source end-of-file (SEOF) sentinel symbol
* '\0' or only one of the folowing constants: 255, 0xFF , EOF * '\0' or only one of the folowing constants: 255, 0xFF , EOF
*/ */
/* Single-lexeme tokens processed separately one by one /* Single-lexeme tokens processed separately one by one
* in the token-driven part of the scanner * in the token-driven part of the scanner
* '=' , ' ' , '(' , ')' , '{' , '}' , == , <> , '>' , '<' , * '=' , ' ' , '(' , ')' , '{' , '}' , == , <> , '>' , '<' ,
* space * space
@ -34,47 +34,88 @@
*/ */
REPLACE *ESN* WITH YOUR ERROR STATE NUMBER //REPLACE *ESN* WITH YOUR ERROR STATE NUMBER
#define ES *ESN* /* Error state */ #define ES -2 /* Error state */
#define IS -1 /* Inavalid state */ #define IS -1 /* Inavalid state */
/* State transition table definition */ /* State transition table definition */
REPLACE *CN* WITH YOUR COLUMN NUMBER //REPLACE *CN* WITH YOUR COLUMN NUMBER
#define TABLE_COLUMNS *CN* #define TABLE_COLUMNS 14
/*transition table - type of states defined in separate table */ /*transition table - type of states defined in separate table */
int st_table[ ][TABLE_COLUMNS] = { int st_table[][TABLE_COLUMNS] = {
/* State 0 */ {YOUR INITIALIZATION}, /* INPUT COLUMNS:
/* State 1 */ {YOUR INITIALIZATION}, [a-zA-Z]| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | . | # | other
. */
. YOUR TABLE INITIALIZATION HERE /* State 0 */ {1, 6 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , IS , IS ,IS},
. /* State 1 */ {1, 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , ES , 3 , 2},
/* State N */ {YOUR INITIALIZATION}, /* State 2 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS},
/* State 3 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS},
/* State 4 */ {ES, 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 7 , 5 , 5},
/* State 5 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS},
/* State 6 */ {ES, 9 , 9, 9, 9, 9, 9, 9, 9, ES, ES, 7 , ES , 5},
/* State 7 */ {ES, 7 , 7, 7, 7, 7, 7, 7, 7, 7, 7, ES , 8 , 8},
/* State 8 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS},
/* State 9 */ {ES, 9 , 9, 9, 9, 9, 9, 9, 9, ES, ES, ES , ES , 10},
/* State 10 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS},
/* State 11 */ {ES, ES , ES, ES, ES, ES, ES, ES, ES, ES, ES, ES , ES , ES},
/* State 12 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS},
/* State 13 */ {IS, IS , IS, IS, IS, IS, IS, IS, IS, IS, IS, IS , IS , IS}
//
//. YOUR TABLE INITIALIZATION HERE
//.
///* State N */ {YOUR INITIALIZATION},
};
/* Accepting state table definition */ /* Accepting state table definition */
REPLACE *N1*, *N2*, and *N3* WITH YOUR NUMBERS //REPLACE *N1*, *N2*, and *N3* WITH YOUR NUMBERS
#define ASWR *N1* /* accepting state with retract */ #define ASWR 1 /* accepting state with retract */
#define ASNR *N2* /* accepting state with no retract */ #define ASNR 2 /* accepting state with no retract */
#define NOAS *N3* /* not accepting state */ #define NOAS 0 /* not accepting state */
int as_table[ ] = {YOUR INITIALIZATION HERE - USE ASWR, ASNR, NOAS }; int as_table[] = {
/* State 0 */ NOAS,
/* State 1 */ NOAS,
/* State 2 */ ASWR,
/* State 3 */ ASNR,
/* State 4 */ NOAS,
/* State 5 */ ASWR,
/* State 6 */ NOAS,
/* State 7 */ NOAS,
/* State 8 */ ASWR,
/* State 9 */ NOAS,
/* State 10 */ ASWR,
/* State 11 */ ASNR,
/* State 12 */ ASNR,
/* State 13 */ ASWR
};
/* Accepting action function declarations */ /* Accepting action function declarations */
FOR EACH OF YOUR ACCEPTING STATES YOU MUST PROVIDE //FOR EACH OF YOUR ACCEPTING STATES YOU MUST PROVIDE
ONE FUNCTION PROTOTYPE. THEY ALL RETURN Token AND TAKE //ONE FUNCTION PROTOTYPE. THEY ALL RETURN Token AND TAKE
ONE ARGUMENT: A string REPRESENTING A TOKEN LEXEME. //ONE ARGUMENT: A string REPRESENTING A TOKEN LEXEME.
Token aa_funcXX(char *lexeme); // Example: Token aa_funcXX(char *lexeme);
Replace XX with the number of the accepting state: 02, 03 and so on. Token aa_func02(char* lexeme); // VID AVID/KW
Token aa_func03(char *lexeme); // VID SVID
Token aa_func05(char *lexeme); // DIL
Token aa_func08(char *lexeme); // FPL
Token aa_func10(char *lexeme); // OIL
Token aa_func11(char *lexeme); // ES1 (Nothing here)
Token aa_func12(char *lexeme); // ES2
Token aa_func13(char *lexeme); // ES3
//Replace XX with the number of the accepting state: 02, 03 and so on.
/* defining a new type: pointer to function (of one char * argument) /* defining a new type: pointer to function (of one char * argument)
returning Token returning Token
*/ */
typedef Token (*PTR_AAF)(char *lexeme); typedef Token(*PTR_AAF)(char *lexeme);
/* Accepting function (action) callback table (array) definition */ /* Accepting function (action) callback table (array) definition */
@ -82,14 +123,28 @@ typedef Token (*PTR_AAF)(char *lexeme);
* Token (*aa_table[])(char lexeme[]) = { * Token (*aa_table[])(char lexeme[]) = {
*/ */
PTR_AAF aa_table[ ] ={ PTR_AAF aa_table[] = {
/* State 0 */ NULL,
/* State 1 */ NULL,
/* State 2 */ aa_func02,
/* State 3 */ aa_func03,
/* State 4 */ NULL,
/* State 5 */ aa_func05,
/* State 6 */ NULL,
/* State 7 */ NULL,
/* State 8 */ aa_func08,
/* State 9 */ NULL,
/* State 10 */ aa_func10,
/* State 11 */ aa_func11,
/* State 12 */ aa_func12,
/* State 13 */ aa_func13
HERE YOU MUST PROVIDE AN INITIALIZATION FOR AN ARRAY OF POINTERS //HERE YOU MUST PROVIDE AN INITIALIZATION FOR AN ARRAY OF POINTERS
TO ACCEPTING FUNCTIONS. THE ARRAY HAS THE SAME SIZE AS as_table[ ]. //TO ACCEPTING FUNCTIONS. THE ARRAY HAS THE SAME SIZE AS as_table[ ].
YOU MUST INITIALIZE THE ARRAY ELEMENTS WITH THE CORRESPONDING //YOU MUST INITIALIZE THE ARRAY ELEMENTS WITH THE CORRESPONDING
ACCEPTING FUNCTIONS (FOR THE STATES MARKED AS ACCEPTING IN as_table[]). //ACCEPTING FUNCTIONS (FOR THE STATES MARKED AS ACCEPTING IN as_table[]).
THE REST OF THE ELEMENTS MUST BE SET TO NULL. //THE REST OF THE ELEMENTS MUST BE SET TO NULL.
}; };
@ -97,7 +152,7 @@ THE REST OF THE ELEMENTS MUST BE SET TO NULL.
#define KWT_SIZE 8 #define KWT_SIZE 8
char * kw_table []= { char * kw_table[] = {
"ELSE", "ELSE",
"IF", "IF",
"INPUT", "INPUT",
@ -106,7 +161,6 @@ char * kw_table []= {
"REPEAT", "REPEAT",
"THEN", "THEN",
"USING" "USING"
}; };
#endif #endif