📄 preccx.1
字号:
libcc.a has been placed..PPNote that `\\x\0{@ $x @}' has no real effect, so it has been droppedfrom most of the points in the script where it might have been expected..PPHere is the same script, but suitably coded for versions of\fIpreccx\fP up to 2.40..IP# define TOKEN char.IP# define VALUE int.IP# define BEGIN call_mode=0;printf("\\nready> ");.IP#\0include\0"cc.h".IP#\0include\0<ctype.h>.IPstatic int acc;.IP@\0digit\0=\0(isdigit)\0\0\0\0\0\0{: $$=$1-'0';.IP@\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0acc=acc*10+$1;:}.IP@\0posint=\0digit\0posint\0\0\0{:$$=$2; :}.IP@\0\0\0\0\0\0\0|\0digit\0\0\0\0\0\0\0\0\0\0{: $$=$1;acc=0; :}.IP@\0int\0\0\0=\0<'-'>\0posint\0\0\0{: $$=-$2; :}.IP@\0\0\0\0\0\0\0|\0posint.IP@\0atom\0\0=\0<'('>expr<')'>\0{: $$=$2; :}.IP@\0\0\0\0\0\0\0|\0int.IP@\0expr\0\0=\0atom\0sign_sum\0\0{: $$=$1+$2; :}.IP@\0\0\0\0\0\0\0|\0atom.IP@\0sign_sum=\0<'-'>\0atom\0sign_sum.IP@\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0{: $$=-$1+$3; :}.IP@\0\0\0\0\0\0\0\0\0|\0<'-'>\0atom\0\0\0{: $$=-$2; :}.IP@\0\0\0\0\0\0\0\0\0|\0<'+'>\0atom\0sign_sum.IP@\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0{: $$=$1+$3; :}.IP@\0\0\0\0\0\0\0\0\0|\0<'+'>\0atom\0\0\0{: $$=$2; :}.IP@\0top\0\0\0\0\0=\0expr\0\0\0\0\0\0\0\0\0{: printf("=%d\\n",$1); :}.IPMAIN(top).PPFor an example of a parser which uses parameters essentially, the followingdefinition of a parser which accepts only the fibonacci sequence asinput may be useful:.IP# define TOKEN char.IP# define VALUE char*.IP# include "ccx.h".IP# include <math.h>.IP# define INT(x) (int)(x).IP# define DIV(m,n) INT(INT(m)/INT(n)).IP# define MOD(m,n) INT(INT(m)%INT(n)).IP# define LOG10(n) INT(log10((double)(n))).IP# define DBLE(n) (double)(n).IP# define TEN DBLE(10).IP#\0define\0FIRSTDIGIT(n)\0\\(0!=n)?DIV((n),pow(TEN,DBLE(LOG10(n)))):0.IP#\0define\0LASTDIGITS(n)\0\\(0!=n)?MOD((n),pow(TEN,DBLE(LOG10(n)))):0.IPMAIN(fibber).IP\0@fibber\0\0\0=\0{\0fibs\0$!\0}*.IP\0@fibs\0\0\0\0\0=\0fib((PARAM)1,(PARAM)1)\k.IP\0@\0\0\0\0\0\0\0\0\0\0\0{:\0printf("%d\0terms\0OK\\n",(int)$k);\0:}.IP\0@fib(a,b)\0=\0number(a)\0<','>\0fib(b,a+b)\k\0{@\0$k+1\0@}.IP\0@\0\0\0\0\0\0\0\0\0|\0<'.'>\0<'.'>\0\0\0\0\0{@\00\0@}.IP\0@\0\0\0\0\0\0\0\0\0\0{:\0printf("Next\0terms\0are\0%d,%d,..\\n",(int)a,(int)b);\0:}.IP\0@number(n)=\0digit(n).IP\0@\0\0\0\0\0\0\0\0\0|\0digit(FIRSTDIGIT(n))\0number(LASTDIGITS(n)).IP\0@digit(n)\0=\0<n+'0'>\0\0/*\0rep.\0of\01\0digit\0n\0*/.PPThe\0following\0are\0some\0example\0inputs\0and\0responses:.IP1,1,2,3,5,..\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0Next\0terms\0are\08,13,..\0\0\0\0\0\0\0\0\0\0\0\0\0\0\05\0terms\0OK\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.IP1,1,2,3,5,8,13,21,34,51,85,..\0\0\0\0\0\0\0\0\0\0\0\0\0error:\0failed\0parse:\0probable\0error\0at\0<>1,85,..\0.SH\0FILESThe following files are to be found in the \fI/users/news/preccx\fP directory:.PD 0.TP 15preccxPreccx executable.TP 15preccx.yPreccx definition in its own language.TP 15lex.yTokenizer for \fIpreccx\fP.TP 15c.yC parser for \fIpreccx\fP.TP 15preccx.cPreccx C source code (generated by \fIpreccx\fP from \fIpreccx\fP.y)..TP 15preccx.hPreccx header file, needed only to construct \fIpreccx\fP..TP 15preamble.cAuxiliary functions, needed only to construct \fIpreccx\fP..TP 15preamble.hHeader file for preamble.c, needed only to construct \fIpreccx\fP..TP 15common.cSimple parsers common to both non-parameterised and parameterised parserkernels. Needed to make \fIcommon.o\dP, included in libcc.a..TP 15engine.cRuntime engine. Needed to make \fIengine.o\dP, included in libcc.a..TP 15ccx.cThe source code of the \fIpreccx\fP 1.0 kernel operations, needed tomake \fIccx.o\fP, included in libcc.a..TP 15cc.cThe source code of the unparameterized \fIpreccx\fP 1.0 kernel operations,needed to make \fIcc.o\fP, included in libcc.a..TP 15ccx.hThe header file of the \fIpreccx\fP parameterized kernel operations, neededby codes generated by \fIpreccx\fP..TP 15cc.hThe header file of the unparameterized \fIpreccx\fP kernel operations,an alternative to ccx.h if you do not use parameterized definitions..TP 15yystuff.cDefault lexer which allows you to escape newlines..TP 15on_error.cDefault error routines..TP 15atexit.cIn case atexit() is not present on your system..TP 15libcc.aThe library containing cc.o, ccx.o and yystuff.o, needed to compile anexecutable from code built by \fIpreccx\fP..TP 15MakefileThe makefile for \fIpreccx\fP..TP 15test.ySimple test script for \fIpreccx\fP..TP 15test.cC output from the \fItest.y\fP script..TP 15testThe test parser built by `gcc \-ansi \-o test test.c \-L ... \-lcc'..PD.SH "SEE ALSO"yacc(1),lex(1),gcc(1L),.SH AUTHORPeter Breuer, Programming Research Group,Oxford University Computing Laboratory, UK..brMan page also hacked by Jonathan Bowen..SH BUGS1. On Sun3's, the \fIgcc\fP compiler still complains that \fIprintf\fPis being redefined. I don't know why. If anyone finds the rightcompiler switch to magic this away, please tell me! For the hp300series, the switch is \-D__hp9000s300, if that's any clue?.PP2. (Cured Mar 10 1992 in v1.1).PP3. If you drastically change the type of VALUE in yourscript (make it larger than char*), you will also have to change the typedeclared in cc.h and then recompile the libcc.a library. This is not a bugbut a feature..PP4. (Cured Mar 17 1992 in v1.2)..PP5. It has been reported that the IBM `ANSI' C compiler does not like the.IPtypedef STATUS PARSER();.PPdefinition made by \fIpreccx\fP. That is their problem..PP7. (patch issued for \fIpreccx\fP 2.30+ April 15 1993). Error in p_uniq0code prevented recognition of all backtrack errors, with the effectthat they were caught as failed parses some time later instead..PP8. (patch issued for 2.40+ July 1994). \fIPreccx\fP's C expresions don'tpermit the use of `.' as an operator. My omission. Use a macroinstead until corrected (corrected)..PPPlease report problems to \fI<Peter.Breuer@comlab.ox.ac.uk>\fP..SH NOTESIn version 1.30 and above, script lines can be continued by placing an `@'at the beginning of the next line, without a `\\' at the end of theprevious line. Each sequence of `@' continued lines must be terminatedby an empty line..PPVersion 1.40 introduced a hook TOKEN *yybuffer for external lexical analysers.This is where lexers must eventually write their output for \fIpreccx\fP to seeit. Version 2.0 and above use a special routine mygets() to callyylex() which places the TOKEN returned by yylex() in the right placeautomatically. For backwards compatibility, it is still possible towrite into yybuffer directly, however. Note that, as mentioned already,EOF is tested by looking at the global int yytchar.The default yylex() lexer in libcc.a handles all this correctly. Version 2.41 and above adds a special call get1token() which is usedin mygets() to get one token from yylex(). You can use it to skip atoken by calling it from an error handler instead of within mygets().All calls to the lexer now go through get1token() and all interactionswith the buffer go through get1token() and realignbuffer()..PPThe default zer_error() handler supplied with preccx simply prints an errormessage and the unparsed portion of the string. That might well be\fIall\fP of the string, since \fIpreccx\fP parsers try their darn'destto make a match, then backtrack, so the (TOKEN *)maxp pointer is provided. Thispoints to the deepest successful penetration into the incoming string, and isusually the point to look for the error. The pointer (TOKEN *)pstr showsthe unparsed string, of which (TOKEN *)maxp will be an end-segment (thelast TOKEN, in fact)..PPIf you want to try and resync the parse at an error, a sensible thing todo would be to (rewrite zer_error to) skip a token at maxp, and rerun the parse.You will have to read the code of the run() function defined in \fBcc.c\fP tomake sense of it, but you might try:.IPstrcpy(maxp,maxp+1);tok=the_top_level_parser();.IPif(GOODSTATUS(tok)){.IPpc=0;pc=p_evaluate());.IP}.IPelse\0printf("At\0least\0I\0tried!\\n"));.PPUsing a counter to set a maximal number of resync attempts in a single linewould also be sensible!.PPYou can obviate any bad_error() call bymaking sure that the top-level parser has a failsafe fallthrough toa ?* parser, with some kind of error action attached..PPThe version 2.x series \fIpreccx\fP extended version 1.x by allowingparameters to each clause of the grammar (i.e., it treats inheritedattribute grammars as well as synthetic ones), and by introducing the`!' (cut) marker. This can be inserted in expressions in order to stopbacktracking through that point, which is useful in avoidingexcessively long searches for alternate parses when no alternate ispossible..PPPromises: version 2.x will eventually eliminate the archaic\fIyacc\fP-style of stack manipulation with something much nicer(achieved in 2.4x series). Version 3.0 should implement tighttype-checking. Contact the author for the most recent version.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -