The Definitive Antlr 4 第5章学习笔记

本文介绍了ANTLR4中词法分析与语法分析的基础概念及应用技巧。包括如何定义标识符、数字、字符串等基本元素,处理注释与空白符,以及如何通过递归定义词法规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 The Definitive Antlr 4 Reference 2nd Edition  第5章学习笔记


antlr中所生成的parser用来处理token stream 而lexer用来处理char stream。Antlr中,文法与词法有类似的结构,因此可以将二者写在一个文件中。但词法分析与语法分析属于不同的阶段,所以需要告诉Antlr什么阶段进行语法分析,什么阶段进行词法分析。因此以大写字母开头的表示词法分析规则,以小写字母开头的表示语法分析规则。

例如下面定义了函数调用的语法与词法规则。

call : ID LP exprList RP ;
LP : '(' ;
RP : ')' ;
下面是本章的一些语法与词法定义的案例。

匹配字符串

这里字符串是由双引号""包含的字符序列,例如STRING:"...",对应文法可以写为 ".*?"。.匹配任意单个字符,在Antlr中支持非贪婪匹配,即满足条件的最小匹配。使用?为后缀,表示非贪婪匹配。.*贪婪匹配,加上后缀.*?表示非贪婪匹配".*?" 模式处理的字符串,不允许在字符串内部有双引号,为支持引号,需要使用转义符,文法如下:

expr : STRING;
STRING: '"' (ESC|.)*? '"';
fragment
ESC : '\\"' | '\\\\';

由于转意符本身需要转意 因此使用 \\,当遇到未转义的"号,则字符串匹配结束。



匹配注释与空白符

在有些应用中,需要匹配空白符或删除空白符。如在赋值表达式中, a =     789;     = 号两侧是可以有若干的空格存在。

当希望丢弃匹配到的空白符,可以使用skip命令。

LINE_COMMENT : '//' .*? '\r'? '\n' -> skip ; 忽略单行注释。
COMMENT : '/*' .*? '*/' -> skip ; 多行注释会被忽略



常用的词法定义

标示符定义:以C语言为例,C中变量标示符以字母下划线开头,随后可以跟若干个字母数字下划线。

expr :  ID;
ID : ID_LETTER (ID_LETTER | DIGIT)*;

fragment ID_LETTER: [a-zA-Z_];
fragment DIGIT : [0-9];


数字定义: 通常有整数与浮点数的定义。

INT :  DIGIT+ ;
FLOAT: DIGIT+ '.' DIGIT*
      | '.' DIGIT+
      ;

字符串定义:由双引号包围的字符串定义。

STRING :'"' ( ESC | . )*? '"' ;
fragment ESC :'\\' [btnr"\\] ; // \b, \t, \n etc...



注释的定义:  匹配并丢弃注释

LINE_COMMENT : '//' .*? '\n' -> skip ;
COMMENT : '/*' .*? '*/' -> skip ;

空白符号的定义:

由回车、换行、制表符构成。

WS : [ \t\n\r]+ -> skip ;

调用语句定义:

call : ID'('exprList')';
exprList : | ID (','ID)*;
ID : ID_LETTER (ID_LETTER | DIGIT)*;
WS : [ \t\n\r]+->skip;
fragment ID_LETTER: [a-zA-Z_];
fragment DIGIT : [0-9];<strong>



词法分析与语法分析器的关联

由于在Antlr中词法规则可以使用递归定义,词法与语法规则定义同样强大。这样,在词法定义中甚至可以匹配程序结构。相反,也可以将语法分析中的符号元素看作是字符的集合,然后对这些字符进行语法分析。


下面是一些使用Antlr的一些经验:

  • 由于在语法分析中某些符号并不需要,因此在词法分析中匹配并丢弃这些符号。例如注释空白符等。
  • 通过词法分析来匹配符号,如标示符,关键字,字符串,数字等。因为语法分析的开销比词法分析更大,因此不应该让语法分析器来完成词法分析任务。
  • 将词法分析器所处理的元素当作一个整体来对待。例如,词法分析并不关系XML标签的内容,因此词法分析器可以将<>内的元素看做一个整体识别,并作为Tag(标签)来对待。

此外,如果是语法分析器需要处理的元素,那么词法分析器应该将这些元素单独传给语法分析器。例如分析IP地址,那么IP地址中的数字与符号(.)应该单独作为词法单元,最后传给语法分析器。


有时候语法分析并不需要分析某些结构,例如一个日志文件中 

192.168.209.85"GET /download/foo.html HTTP/1.0" 200 

这样的数据有多少行。由于仅仅是分析有多少行,因此仅需要考虑\n 换行符号,其它元素可以直接忽略掉。

因此这样的一个文件的文法可以是:

file : NL+ ;
STUFF : ~'\n'+-> skip ; // 除了换行以外的字符全部忽略
NL : '\n' ; // 换行符号的定义

~x 意思是匹配除了x以外的所有字符。



Programmers run into parsing problems all the time. Whether it's a data format like JSON, a network protocol like SMTP, a server configuration file for Apache, a PostScript/PDF file, or a simple spreadsheet macro language--ANTLR v4 and this book will demystify the process. ANTLR v4 has been rewritten from scratch to make it easier than ever to build parsers and the language applications built on top. This completely rewritten new edition of the bestselling Definitive ANTLR Reference shows you how to take advantage of these new features. Build your own languages with ANTLR v4, using ANTLR's new advanced parsing technology. In this book, you'll learn how ANTLR automatically builds a data structure representing the input (parse tree) and generates code that can walk the tree (visitor). You can use that combination to implement data readers, language interpreters, and translators. You'll start by learning how to identify grammar patterns in language reference manuals and then slowly start building increasingly complex grammars. Next, you'll build applications based upon those grammars by walking the automatically generated parse trees. Then you'll tackle some nasty language problems by parsing files containing more than one language (such as XML, Java, and Javadoc). You'll also see how to take absolute control over parsing by embedding Java actions into the grammar. You'll learn directly from well-known parsing expert Terence Parr, the ANTLR creator and project lead. You'll master ANTLR grammar construction and learn how to build language tools using the built-in parse tree visitor mechanism. The book teaches using real-world examples and shows you how to use ANTLR to build such things as a data file reader, a JSON to XML translator, an R parser, and a Java class->interface extractor. This book is your ticket to becoming a parsing guru! What You Need: ANTLR 4.0 and above. Java development tools. Ant build system optional (needed for building ANTLR from source)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值