in reply to Re^2: command input processing
in thread command input processing
I probably more curious as to how languages say perl, intepret language structure/grammar
Ignore perlfunc functions for a moment. What you describe and perl don't compare. In your scenario, the script calls functions defined in the language itself. In perl, the script calls functions the script defines. Perl doesn't care what the function is called, or how many arguments it has, or anything, because all functions look the same to perl.
Except, of course, builtin functions, which perlfunc functions may be. I don't know how that's done. C, C++ and Java don't have any builtin functions as far as I know. There's "new", but it's an operator. And yeah, there's surely a big code or data structure to parse the operators.
What compilers do is make a tree. In compilers that create binaries or bytecode, the tree is serialized into instructions. perl keeps the program in tree form. For perl, executing the program is simply navigating the tree and taking actions based on the type of node it finds. Here's an example:
>perl -MO=Terse -e "$a = $a + 1" LISTOP (0x18a01ec) leave [1] OP (0x18a01d0) enter COP (0x18a0210) nextstate BINOP (0x18a024c) sassign BINOP (0x18a0270) add [3] UNOP (0x18a02b4) null [15] PADOP (0x18a02d4) gvsv 2 SVOP (0x18a0294) const SPECIAL #0 Nullsv UNOP (0x18a02f4) null [15] PADOP (0x18a0314) gvsv 1 -e syntax OK
It takes lots of code and/or lots of tables to build this tree. 387772 is a parser I wrote for a relatively simple grammar. Look how long it is, and the language it parses only has conditionals, loops, line continations, literals and variables, nothing else. There are tools to assist you, such as lex+yacc and Parser::RecDescent, but it's still a lot of work. (Coding is only a small portion of it, too. The amount of thought that should be put in the design of a language is enourmous, but that's off topic.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4:command input processing
by thekestrel (Friar) on Oct 22, 2004 at 14:12 UTC | |
by ikegami (Patriarch) on Oct 22, 2004 at 17:46 UTC | |
by thekestrel (Friar) on Oct 24, 2004 at 10:43 UTC |