Parsing a file in C

Programming, for all ages and all languages.
Post Reply
guest

Parsing a file in C

Post by guest »

how to parse a file in C ?
of course thats for compiler.
Schol-R-LEA

Re:Parsing a file in C

Post by Schol-R-LEA »

You'll have to give a bit more information about the goal. I'm afraid. Are you parsing a file of some given format or language using C, or are you parsing a C source file (whether using C or some other language) for compilation? If the former, what is the file format or grammar? If the latter, do you intend to write a hand-coded parser, or can you use a parser generator like Bison?
guest

Re:Parsing a file in C

Post by guest »

Hello,
I don't want to use parser like Bison i want to write my own.
I want to do somthing like that :

echo ("hello");
if (x=2) {
echo ("2");
};

i want the parser reads the file when gets echo then reads the text between ("") and record it in a x then send x to echo(x) function.

And do the same in the next line.
Each line it reads when it finds ; then there will be the end of the line.
Schol-R-LEA

Re:Parsing a file in C

Post by Schol-R-LEA »

I think I see... you want to write an interpreter for a simple language of some kind, right? Hmmmn... there's just no way to explain enough of that in a single post, I'm afraid. I'd reccommend you look through a few of the older threads (I'll list several below) which have both information and links on these topics.

Making a Compiler?
OS Dev Compiler
Finding Simple Codes of Compiler
Making your own programming language?
Language Design: Basis
Language Design:Sibilance
Language Design: Obscene

I can tell you that you'll have to start by designing the language itself; it doesn't have to fancy, but you will want at least a simple grammar for it. From what I've seen, a BNF grammar for what you want might go like:

Code: Select all

<script> ::= <statement>*

<statement> ::= <expression> ";"
                          | <block>

<expression> ::= <echo>
                   | <if statement>
                   | <block>

<block> ::= "{" <statement>+ "}"

<echo> ::= "echo" "(" <value> ")"

<if statement> ::= "if" "(" <boolean clause> ")" <expression>

<boolean clause> ::= <value>
                                 | <value> <comparator> <value>

<comparator> ::= "==" | "<" | ">" | "<=" | ">=" | "!"

<value> ::= <literal> | <variable>

<literal> ::= '"' <string> '"'

<variable> ::= <letter> [<alphanumeric> | "_"]+

<letter> ::= ["A" | "B" | "C" ... |"X" | "Y" | "Z"] | ["a" | "b" | "c" ... |"x" | "y" | z"] 

<numeral> := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
This is just an example from what you've shown use, mind you, and probably isn't what you actually want; you'll have to figure it out yourself what it should be like. In any case, you would use the grammar you come up with as the basis for lexical analyzer and parser.

The lexical analyzer is the easier part; it just reads the source code one char at a time, and builds the individual tokens from it. This usually written as a finite state machine.

For the parser itself, the most likely way is to use the recursive descent method; basically, you would write a function for each non-terminal in the grammar, and try to match it to one of the possible productions until you reach all of the grammar terminals.

You'd still need to interpret the parse tree from this, which is a whole can of worms in and of itself.
Post Reply