6ef1b4c5ac85600365e3fa56fc70f049c9f5de26
2 \brief lexical analyzer implementation for APC
3 \details The lexer manages two FIFO stacks. One for maintaining tokens, the
4 other for maintaining a list of files to be scanned. During
5 execution, the lexer will return a token from its token queue if any
6 are present. If not, the lexer will will pop an element from its
7 file queue to 'scanner' to be tokenized. If the file queue is empty,
8 the lexer will instead call 'parsedir' to traverse the directory tree
9 and tokenize the results. If 'parsedir' does not generate any new
11 \author Jordan Lavatai
13 ----------------------------------------------------------------------------*/
23 #include "parser.tab.h"
25 #define DE_STACKSIZE 1024
28 #define TK_STACKSIZE 1024
33 void lexer_pushtok(int, YYSTYPE
);
35 int lexer_lex(const char*);
36 struct dirent
* lexer_direntpa
[DE_STACKSIZE
], **lexer_direntpp
;
39 int scanner_init(void);
43 int dredge_current_depth(void);
48 { YYSTYPE lval
; //token val
49 int tok_t
; //token type
50 } token_stack
[TK_STACKSIZE
];
53 { int* tpt
; //token pointer type
55 YYSTYPE
* tvp
; //token value pointer
58 /* Directory Entity Array/Stack
59 Simple array for keeping track of dirents yet to be processed by the scanner.
60 If this list is empty and there are no tokens, the lexer is done.
61 This array is populated by the scanner as an array, and popped locally by the
64 #define DE_STACK (lexer_direntpa)
65 #define DE_STACKP (lexer_direntpp)
66 #define DE_LEN() (DE_STACKP - DE_STACK)
67 #define DE_INIT() (DE_STACKP = DE_STACK)
68 #define DE_POP() (*--DE_STACKP)
71 This is a FIFO stack whose pointers are a union of either a pointer to an
72 integer, or a pointer to two integers (a struct tok). This way, integers may
73 be added or removed from the stack either singularly (IPUSH/IPOP), or as a
74 full token of two integers (PUSH/POP).
75 An alignment error will occur if IPOP or IPUSH are used a non-even number of
78 #define TK_STACK (token_stack)
79 #define TK_STACKP (tks.tok)
80 #define TK_STACKPI (tks.tpt)
81 #define TK_STACKPL (tks.tvp)
82 #define TK_STACKX (tkx.tok)
83 #define TK_STACKXI (tkx.tpt)
84 #define TK_LEN() (TK_STACKX - TK_STACKP)
85 #define TK_INIT() (TK_STACKP = TK_STACKX = TK_STACK)
86 #define TK_POP() (*TK_STACKP++)
87 #define TK_POPI() (*TK_STACKPI++);
88 #define TK_POPL() (*TK_STACKPL++);
89 #define TK_PUSH(T,L) (*TK_STACKX++ = (struct tok){L,T})
92 The initializer returns boolean true if an error occurs, which may be handled with standard errno.
98 return scanner_init();
102 If the token buffer is empty, 'lexer' will initialize the token buffer and
103 call 'lexer_scandir'. If SCAN_ERROR is returned, an error is printed
104 before sending a null return to bison. If 0 tokens are generated, the error
105 printing is skipped. In all other cases, 'yylval' is set, and the token's
106 integer representation is returned.
110 #define SCAN_ERROR -1
111 #define TK_EMPTY (TK_STACKP == TK_STACKX)
114 fprintf(stderr,__VA_ARGS__); \
119 while (DE_LEN() > 0) //lex any directory entries in our stack
120 if (lexer_lex(DE_POP()->d_name
) == 0) //fail if it generates no tokens
121 FAIL("Lexer failed to tokenize [%s]\n",(*DE_STACKP
)->d_name
);
122 if (TK_EMPTY
) //if there are no tokens,
123 { TK_INIT(); //initialize the token stack back to 0
125 { case SCAN_ERROR
: //if an error occurred,
126 FAIL("Scanner error\n");
127 case 0: //if the the scanner finds no dirents,
128 goto done
; //then we are done
129 default: //if we found some elements to scan,
130 goto start
; //start over and lex them
142 This receiver takes a struct tok and pushes it to the FIFO stack.
145 #define $($)#$ //stringifier
146 #define ERR_TK "Fatal: Generated over " $(TK_STACKSIZE) " tokens in one pass."
147 ( int tok
, YYSTYPE lval
)
148 { if (TK_LEN() >= TK_STACKSIZE
)
149 { fprintf(stderr
, ERR_TK
);
153 printf("Pushed Token %i | %i\n", TK_STACK
[TK_LEN() - 1].tok_t
, TK_STACK
[TK_LEN() - 1].lval
.val
);