For Programmers: Free Programming Magazines  


Home > Archive > Compilers > April 2004 > latex grammar









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author latex grammar
Lukasz

2004-03-27, 12:29 am

Hello

Where can I find grammar for latex so I can create parser?
Thanks for any advices.

best regars
Lu
Ivan Boldyrev

2004-04-03, 9:32 am

On 8696 day of my life journey@op.pl wrote:
> Hello
>
> Where can I find grammar for latex so I can create parser?
> Thanks for any advices.


LaTeX is based on TeX, which doesn't have contex-free grammar, because
you can change class of every simbol during processing. And packages
can introduce own sublanguages (for example, XY-pic).

Of course, you can limit your tool to "standard" syntax and ignore
such featuers. Then task of creating a grammar is homework-like :)

--
Ivan Boldyrev

Perl is a language where 2 x 2 is not equal to 4.

David Z Maze

2004-04-03, 9:32 am

"Lukasz" <journey@op.pl> writes:

> Where can I find grammar for latex so I can create parser?
> Thanks for any advices.


I suspect you'd have a fair bit of trouble writing a parser for LaTeX
using traditional tools. Fundamentally, LaTeX is just a set of macros
on top of the base TeX language. But that language has some odd
runtime constructs, and lets you change the behavior of the scanner at
runtime. So you can do tricks like

\def\makecommand#1#2{\expandafter\def\csname #1\endcsname{#2}}
\makecommand{foo}{bar}
\foo % evaluates to "bar"

to create a command that defines a symbol based on a name, for
example. (\expandafter means "skip the next symbol momentarily"; then
the runtime evaluates \csname #1\endcsname to produce the symbol; then
the runtime goes back to the \def and now sees \def\foo{bar}.) There
are also irregularities like \verb in the "standard" LaTeX language,
where the delimiter for the command arguments is the next character.

There's probably two ways to go, and it depends on what your goal is.
You could write an evaluator for base TeX with some effort; the
TeXbook is dense but should have the information you need. This could
read in any TeX, LaTeX, texinfo, ... document; if you wanted to
reimplement the layout engine or convert to plain text, this might be
a good way to go. Or, you could write an evaluator that understood
the higher-level LaTeX constructs, which I gather is what the various
LaTeX-to-foo converters generally try to do. This has the advantage
of capturing the semantic meaning in e.g. \section but will work
poorly against documents that use more advanced features (manual
\defs, packages such as tabularx that provide features differently).
For doing this, having a formatted copy of the LaTeX macro source is
helpful; you could probably also find a reference card that lists
"all" of the standard LaTeX commands.

--
David Maze dmaze@mit.edu http://www.mit.edu/~dmaze/
"Theoretical politics is interesting. Politicking should be illegal."
-- Abra Mitchell

Hans Aberg

2004-04-03, 10:36 am

"Lukasz" <journey@op.pl> wrote:

>Where can I find grammar for latex so I can create parser?
>Thanks for any advices.


You can answers from the LaTeX mailing list (send an empty mail or one
with the only word "info" in the body to
<LISTSERV@LISTSERV.UNI-HEIDELBERG.DE> ).

From what recall, though, so did Leslie Lamport, who made the original
LaTeX, base the LaTeX grammar on the Pascal grammar, but as TeX does
not know how it and for other reasons, it has never been published. I
also think that the current LaTeX development team does not use an
explicit grammar for the further development of LaTeX. One really
needs a new TeX, with capability of checking grammars for that to make
sense.

So there is no guarantee that such a grammar will work parsing LaTeX. But
there might be some partial LaTeX grammars around.

Hans Aberg
glen herrmannsfeldt

2004-04-03, 10:36 am

Lukasz wrote:


> Where can I find grammar for latex so I can create parser?
> Thanks for any advices.


I would say that you can't write a grammar for LaTeX, though for
specific problems you might be able to do it.

If you don't allow \newcommand or \renewcommand, or any of the TeX
commands that can define macros, change catcodes, or most of the low
level things that the system will let you do, then it might be
possible.

-- glen
Paolo Bonzini

2004-04-03, 10:36 am

> Where can I find grammar for latex so I can create parser?
> Thanks for any advices.


LaTeX is interpreted and it is pretty much impossible to create a
complete parser for it. It should be possible (and fairly easy, I'd
say) to support a streamlined version without macros (except very
simple ones -- no \ifs, no \futurelet, no \expandafter, no token
registers, etc.), catcodes (again except possibly some easy ones), and
most of TeX complexities (\hbox/\vbox, \halign, only very simple
glues, \specials, leaders, marks, insertions, etc. etc.). In other
words, expect to hardcode in your parser something to support for
every LaTeX package you care about.

Paolo

Theodore Norvell

2004-04-14, 1:31 am

Lukasz wrote:
> Where can I find grammar for latex so I can create parser?
> Thanks for any advices.


I once figured out how to do the lexical analysis for LaTeX assuming
the catcodes are normal. It's likely not complete, but it might
represent a start. I can send a copy of that if you're interested.

Cheers,
Theodore Norvell
James Leifer

2004-04-14, 1:31 am

"Lukasz" <journey@op.pl> writes:

> Where can I find grammar for latex so I can create parser?


Have a look at:

http://pauillac.inria.fr/~maranget/hevea/

It's a latex-to-html translator that is capable of handling many
different latex constructions. You therefore might find its front end
helpful.

Kind regards,
--
James Leifer
INRIA Rocquencourt
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com