⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 用yacclex设计实现小型basic语言.txt

📁 本文包含简单中文说明和详细源代码
💻 TXT
📖 第 1 页 / 共 5 页
字号:
        ;

datatype:CHAR {$$=CHARTYPE;}

        |BYTE {$$=BYTETYPE;}

        |STRING {$$=STRINGTYPE;}

        |INTEGER {$$=INTEGERTYPE;}

        |DWORD {$$=DWORDTYPE;}

        |REAL {$$=REALTYPE;}

        ;

%%

 

void main() {

        InitSymtabRoot();

        ResetIP();

        yyparse();

        exec();

}

 

yyerror(char *s) {

        printf("Error> %s\n",s);

}

 

exec() {

        Reset();

        Start();

/*        ResetIP();*/

 

}

 

W(char *s) {

        printf("%s\n",s);

        fflush(stdout);

}

 

P(void) {

int i;

        for(i=0;i<GetIP();i++)

                printf("%d:%d  ",i,CodeSegment[i]);

}

 

 

 

7、  内存组织和代码生成:我们的内存中有三个逻辑段:数据、堆栈、代码,数据和堆栈共用同一个物理段,栈顶向下生长,代码段单独分开。在语法分析时,向内存中写入指令和数据,在执行时,再读出来。代码生成时如果遇到不可知跳转(如If、While、For等等),就使用预添0技术,先在这个位置填写 Nop,在遇到语句结束后,知道了地址,再在这里添入要跳转的代码。需要注意的是数据/堆栈段的每个内存单元存储的是指向 Variant 数据类型的指针(这台虚拟计算机的每个内存单元都有四个字节大)。

 

#define MAXCODESEGMENTSIZE 65536

#define MAXDATASEGMENTSIZE 65536

 

 

extern DWord CodeSegment[MAXCODESEGMENTSIZE];

extern DWord sIP;

extern PVariant DataSegment[MAXDATASEGMENTSIZE];

 

void WriteCode(DWord c);

void WriteCode2(DWord c1,DWord c2);

void SetCode(DWord offset,DWord op);

DWord GetIP();

void SetIP(DWord ip);

DWord ReadCode();

void ResetIP(void);

 

 

DWord GetFreeDIP();

PVariant GetData(DWord offset);

void SetData(DWord offset,PVariant val);

void InitData(DWord offset,int datatype);

void FreeData(DWord offset);

void InitDataSegment(void);

void ReleaseDataSegment(void);

 

 

程序编译及执行:

首先用 Lex 编译 calc.l 生成lex.yy.c ,然后用 yacc 编译 calc.y 生成 y.tab.h、y.tab.c和y.code.c (如果你没有修改,可省略)。

用 gcc 编译所有c文件。

执行时缺省从 stdin 读入,解释完成后,如果没有错误,就会执行看到结果,如果要执行文件,请使用重定向。

 

大家注意:我们的Basis语言更像Pascal语言。请看例子:

 

例一:

program aaa;

{

dim a as integer;

dim b as real;

dim c as string;

 

a=12;

b=a*2.2;

c="aaa";

c=c+a+b;

? a,b,c;

}

 

例二:

program aaa;

{

dim a as Integer;

dim b as Real;

dim ccc as string;

a=1;

a=a+1;

a=a*(a+20);

a=-a;

b=a;

b=b*2.71828;

 

ccc=ccc+"ASDF"" DED";

dim ddd as real;

ddd=0.123;

ddd=ddd*(ddd-2*ddd);

print a,b,ccc,ddd;

 

}

 

例三:

program aaa;

{

dim a as integer;

a=1;

while a<10  do {

        print a;

        a=a+1;

}

 

}

 

例四:

program t5;

{

dim a as integer;

a=1;

loop:

print a;

a=a+1;

if a<10 then goto loop;

 

print "Done";

 

}

 

例五:

program aaa;

{

dim a as integer;

for a=-2 to 2 do print a*a;

}

 

 

王晓智 1999

 

 

后记:

本来想详细写一下如何使用YACC,但我觉得这些应该是已经有的话题,所以这里将 TPLY 4.1 版本的帮助附在后面,它很详细,我没什么可补充的。

 

自从96.5 第一次遇到 Delphi 1.0 以后,我一直在 Delphi 下编写程序。但我总觉得想做一个合格的 Delphi 程序员,也应该也从其它地方学习,才能有所进步。

 

王晓智 2002.3

 

 

 

 

      TP Lex and Yacc - The Compiler Writer's Tools for Turbo Pascal

      == === === ==== = === ======== ======== ===== === ===== ======

 

                     Version 4.1 User Manual

                     ======= === ==== ======

 

                         Albert Graef

                 Department of Musicinformatics

               Johannes Gutenberg-University Mainz

 

               ag@muwiinfa.geschichte.uni-mainz.de

 

                          April 1998

 

 

Introduction

============

 

This document describes the TP Lex and Yacc compiler generator toolset. These

tools are designed especially to help you prepare compilers and similar

programs like text processing utilities and command language interpreters with

the Turbo Pascal (TM) programming language.

 

TP Lex and Yacc are Turbo Pascal adaptions of the well-known UNIX (TM)

utilities Lex and Yacc, which were written by M.E. Lesk and S.C. Johnson at

Bell Laboratories, and are used with the C programming language. TP Lex and

Yacc are intended to be approximately "compatible" with these programs.

However, they are an independent development of the author, based on the

techniques described in the famous "dragon book" of Aho, Sethi and Ullman

(Aho, Sethi, Ullman: "Compilers : principles, techniques and tools," Reading

(Mass.), Addison-Wesley, 1986).

 

Version 4.1 of TP Lex and Yacc works with all recent flavours of Turbo/Borland

Pascal, including Delphi, and with the Free Pascal Compiler, a free Turbo

Pascal-compatible compiler which currently runs on DOS and Linux (other ports

are under development). Recent information about TP Lex/Yacc, and the sources

are available from the TPLY homepage:

 

   http://www.musikwissenschaft.uni-mainz.de/~ag/tply

 

For information about the Free Pascal Compiler, please refer to:

 

   http://www.freepascal.org

 

TP Lex and Yacc, like any other tools of this kind, are not intended for

novices or casual programmers; they require extensive programming experience

as well as a thorough understanding of the principles of parser design and

implementation to be put to work successfully. But if you are a seasoned Turbo

Pascal programmer with some background in compiler design and formal language

theory, you will almost certainly find TP Lex and Yacc to be a powerful

extension of your Turbo Pascal toolset.

 

This manual tells you how to get started with the TP Lex and Yacc programs and

provides a short description of these programs. Some knowledge about the C

versions of Lex and Yacc will be useful, although not strictly necessary. For

further reading, you may also refer to:

 

- Aho, Sethi and Ullman: "Compilers : principles, techniques and tools."

  Reading (Mass.), Addison-Wesley, 1986.

 

- Johnson, S.C.: "Yacc - yet another compiler-compiler." CSTR-32, Bell

  Telephone Laboratories, 1974.

 

- Lesk, M.E.: "Lex - a lexical analyser generator." CSTR-39, Bell Telephone

  Laboratories, 1975.

 

- Schreiner, Friedman: "Introduction to compiler construction with UNIX."

  Prentice-Hall, 1985.

 

- The Unix Programmer's Manual, Sections `Lex' and `Yacc'.

 

 

Credits

-------

 

I would like to thank Berend de Boer (berend@pobox.com), who adapted TP Lex

and Yacc to take advantage of the large memory models in Borland Pascal 7.0

and Delphi, and Michael Van Canneyt (Michael.VanCanneyt@fys.kuleuven.ac.be),

the maintainer of the Linux version of the Free Pascal compiler, who is

responsible for the Free Pascal port. And of course thanks are due to the many

TP Lex/Yacc users all over the world for their support and comments which

helped to improve these programs.

 

 

Getting Started

---------------

 

Instructions on how to compile and install TP Lex and Yacc on all supported

platforms can be found in the README file contained in the distribution.

 

Once you have installed TP Lex and Yacc on your system, you can compile your

first TP Lex and Yacc program expr. Expr is a simple desktop calculator

program contained in the distribution, which consists of a lexical analyzer in

the TP Lex source file exprlex.l and the parser and main program in the TP

Yacc source file expr.y. To compile these programs, issue the commands

 

   lex exprlex

   yacc expr

 

That's it! You now have the Turbo Pascal sources (exprlex.pas and expr.pas)

for the expr program. Use the Turbo Pascal compiler to compile these programs

as usual:

 

   tpc expr

 

(Of course, the precise compilation command depends on the type of compiler

you are using. Thus you may have to replace tpc with bpc, dcc or dcc32,

depending on the version of the Turbo/Borland/Delphi compiler you have, and

with ppc386 for the Free Pascal compiler. If you are using TP Lex and Yacc

with Free Pascal under Linux, the corresponding commands are:

 

   plex exprlex

   pyacc expr

   ppc386 expr

 

Note that in the Linux version, the programs are named plex and pyacc to

avoid name clashes with the corresponding UNIX utilities.)

 

Having compiled expr.pas, you can execute the expr program and type some

expressions to see it work (terminate the program with an empty line). There

is a number of other sample TP Lex and Yacc programs (.l and .y files) in the

distribution, including a TP Yacc cross reference utility and a complete

parser for Standard Pascal.

 

The TP Lex and Yacc programs recognize some options which may be specified

anywhere on the command line. E.g.,

 

   lex -o exprlex

 

runs TP Lex with "DFA optimization" and

 

   yacc -v expr

 

runs TP Yacc in "verbose" mode (TP Yacc generates a readable description of

the generated parser).

 

The TP Lex and Yacc programs use the following default filename extensions:

- .l:   TP Lex input files

- .y:   TP Yacc input files

- .pas: TP Lex and Yacc output files

 

As usual, you may overwrite default filename extensions by explicitly

specifying suffixes.

 

If you ever forget how to run TP Lex and Yacc, you can issue the command lex

or yacc (resp. plex or pyacc) without arguments to get a short summary of the

command line syntax.

 



 

 

TP Lex

======

 

This section describes the TP Lex lexical analyzer generator.

 

 

Usage

-----

 

lex [options] lex-file[.l] [output-file[.pas]]

 

 

Options

-------

 

-v  "Verbose:" Lex generates a readable description of the generated

    lexical analyzer, written to lex-file with new extension `.lst'.

 

-o  "Optimize:" Lex optimizes DFA tables to produce a minimal DFA.

 

 

Description

-----------

 

TP Lex is a program generator that is used to generate the Turbo Pascal source

code for a lexical analyzer subroutine from the specification of an input

language by a regular expression grammar.

 

TP Lex parses the source grammar contained in lex-file (with default suffix

.l) and writes the constructed lexical analyzer subroutine to the specified

output-file (with default suffix .pas); if no output file is specified, output

goes to lex-file with new suffix .pas. If any errors are found during

compilation, error messages are written to the list file (lex-file with new

suffix .lst).

 

The generated output file contains a lexical analyzer routine, yylex,

implemented as:

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -