方舟编译器课程笔记

词法、语法分析实现方法之分

  • 使用lex、yacc或相似的生成器构建
  • 手写语法分析器、语法生成器

初始化项目

建立项目文件夹git

mkdir bace03
cd bace03

初始化git仓库。web

git init

建立测试用例bash

mkdir test
git add test/
vi test/square.pl0

添加文件:ide

VAR x, squ;

PROCEDURE square;
BEGIN
   squ:= x * x
END;

BEGIN
   x := 1;
   WHILE x <= 10 DO
   BEGIN
      CALL square;
      ! squ;
      x := x + 1
   END
END.

提交任务svg

git commit -am 'add a new test case'

配置git快捷操做测试

git config --global alias.st status
git config --global alias.d diff

实现一个flex demo

在课程中没有提供flex的文档地址:这里附上flex Mannerflex

%%
username    printf("%s", getlogin() );

这里必定要加最后一个空行,不然会报错this

编译运行。spa

flex src/scanner.l
gcc lex.yy.c -ll

我使用的是Mac OS系统,在这里没法使用-lfl参数进行编译,须要改成-ll参数。code

使用Makefile进行编译

all:
	flex src/scanner.l
	gcc lex.yy.c -ll
	./a.out < test/square.pl0

最后使用的代码

/* scanner for a toy Pascal-like language */

%{
/* need this for the call to atof() below */
#include <math.h>
%}

DIGIT    [0-9]
ID       [a-z][a-z0-9]*

%%

{DIGIT}+    {
            printf( "An integer: %s (%d)\n", yytext,
                    atoi( yytext ) );
            }

{DIGIT}+"."{DIGIT}*        {
            printf( "A float: %s (%g)\n", yytext,
                    atof( yytext ) );
            }

if|then|begin|end|procedure|function        {
            printf( "A keyword: %s\n", yytext );
            }

{ID}        printf( "An identifier: %s\n", yytext );

"+"|"-"|"*"|"/"   printf( "An operator: %s\n", yytext );

"{"[^}\n]*"}"     /* eat up one-line comments */

[ \t\n]+          /* eat up whitespace */

.           printf( "Unrecognized character: %s\n", yytext );

%%

main( argc, argv )
int argc;
char **argv;
    {
    ++argv, --argc;  /* skip over program name */
    if ( argc > 0 )
            yyin = fopen( argv[0], "r" );
    else
            yyin = stdin;

    yylex();
    }