Server IP : 85.214.239.14 / Your IP : 18.188.69.167 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/share/doc/libparse-recdescent-perl/examples/ |
Upload File : |
use v5.10; use warnings; use Parse::RecDescent; $RD_TRACE = 1; my $parser = Parse::RecDescent->new(<<'EOGRAMMAR'); <autotree> file: element(s) element: command | literal command: '\\' literal options(?) args(?) options: '[' option(s? /,/) ']' args: '{' element(s?) '}' option: /[^][\\$&%#_{}~^ \t\n,]+/ literal: /[^][\\$&%#_{}~^ \t\n]+/ EOGRAMMAR local $/; my $tree = $parser->file(<DATA>); $tree->explain(0); sub file::explain { my ($self, $level) = @_; for (@{$self->{'element(s)'}}) { $_->explain($level); print "\n"; } } sub element::explain { my ($self, $level) = @_; ($self->{command}||$self->{literal})->explain($level) } sub command::explain { my ($self, $level) = @_; print "\t"x$level, "Command: $self->{literal}{__VALUE__}\n"; print "\t"x$level, "\tOptions:\n"; $self->{'options(?)'}[0]->explain($level+2) if @{$self->{'options(?)'}}; print "\t"x$level, "\tArgs:\n"; $self->{'args(?)'}[0]->explain($level+2) if @{$self->{'args(?)'}}; } sub options::explain { my ($self, $level) = @_; $_->explain($level) foreach @{$self->{'option(s?)'}}; } sub args::explain { my ($self, $level) = @_; $_->explain($level) foreach @{$self->{'element(s?)'}}; } sub option::explain { my ($self, $level) = @_; print "\t"x$level, "Option: $self->{__VALUE__}\n"; } sub literal::explain { my ($self, $level) = @_; print "\t"x$level, "Literal: $self->{__VALUE__}\n"; } __DATA__ \documentclass[a4paper,11pt]{article} \usepackage{latexsym} \author{D. Conway} \title{Parsing \LaTeX{}} \begin{document} \maketitle \tableofcontents \section{Description} ...is easy \footnote{But not \emph{necessarily} simple}. \end{document}