Advertisement

Lex & Yacc

Started by August 14, 2004 09:44 AM
3 comments, last by Venerable Vampire 20 years, 1 month ago
OK I've been fooling around with Lex and Yacc for a little while. I've written some samples, most notably a little calculator that reads a line at a time from the standard input and perform math operations on it. The question is how do I get Lex to get its input from a file? Source code would be helpful, thanks!
--------------------------------------------------------Life would be so much easier if we could just get the source code.
How about the documentation? Geez.
Advertisement
From the flex manual: (I suppose that's the lexer you're using)

Quote:
...
By default (and for purposes of efficiency), the scanner uses block-reads rather than simple `getc()' calls to read characters from yyin. The nature of how it gets its input can be controlled by defining the YY_INPUT macro. YY_INPUT's calling sequence is "YY_INPUT(buf,result,max_size)". Its action is to place up to max_size characters in the character array buf and return in the integer variable result either the number of characters read or the constant YY_NULL (0 on Unix systems) to indicate EOF. The default YY_INPUT reads from the global file-pointer "yyin".

A sample definition of YY_INPUT (in the definitions section of the input file):

%{
#define YY_INPUT(buf,result,max_size) { int c = getchar(); result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); }
%}
...
-- Rasmus Neckelmann
On any Lex version you should just be able to assign to yyin to change the input stream from standard in to a file. ex:
FILE * file = fopen(filename, "r");if (!file) { /* handle error somehow */ }yyin = file;
Ya, I feel stupid :D
I had the file pointer and everything but my extern declaration was in just the wrong place! O well it's working now. Thanks everyone.
--------------------------------------------------------Life would be so much easier if we could just get the source code.

This topic is closed to new replies.

Advertisement