亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
1234
最近訪問板塊 發(fā)新帖
樓主: oldbeginner
打印 上一主題 下一主題

編譯器學(xué)習(xí)筆記02(世界公民antlr)——2014_2_27 [復(fù)制鏈接]

論壇徽章:
0
31 [報告]
發(fā)表于 2014-03-10 20:28 |只看該作者
**************************
7.3 Implementing Applications with Visitors

訪問
**************************





To use a visitor instead of a listener, we ask ANTLR to generate a visitor
interface, implement that interface, and then create a test rig that calls visit()
on the parse tree. We don’t have to touch the grammar at all.

public class TestPropertyFileVisitor {
    public static class PropertyFileVisitor extends
        PropertyFileBaseVisitor<Void>
    {
        Map<String,String> props = new OrderedHashMap<String, String>();
        public Void visitProp(PropertyFileParser.PropContext ctx) {
            String id = ctx.ID().getText(); // prop : ID '=' STRING '\n' ;
            String value = ctx.STRING().getText();
            props.put(id, value);
            return null; // Java says must return something even when Void
        }
    }

    public static void main(String[] args) throws Exception {
        String inputFile = null;
        if ( args.length>0 ) inputFile = args[0];
        InputStream is = System.in;
        if ( inputFile!=null ) {
            is = new FileInputStream(inputFile);
        }
        ANTLRInputStream input = new ANTLRInputStream(is);
        PropertyFileLexer lexer = new PropertyFileLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PropertyFileParser parser = new PropertyFileParser(tokens);
        ParseTree tree = parser.file();

        PropertyFileVisitor loader = new PropertyFileVisitor();
        loader.visit(tree);
        System.out.println(loader.props); // print results
    }
}




   

論壇徽章:
0
32 [報告]
發(fā)表于 2014-03-11 18:12 |只看該作者
本帖最后由 oldbeginner 于 2014-03-11 18:14 編輯

**********************************
7.4 Labeling Rule Alternatives for Precise Event Methods

大人,你能說清楚點嗎?
**********************************





To illustrate the event granularity problem, let’s try to build a simple calculator
with a listener for the following expression grammar:



As it stands, rule e yields a fairly unhelpful listener because all alternatives
of e result in a tree walker triggering the same enterE() and exitE() methods.

看出來了嗎,少數(shù) 規(guī)則e 以權(quán)謀私,即 對應(yīng) 同一個 enterE() 和 exitE() 方法。

public interface ExprListener extends ParseTreeListener {

void enterE(ExprParser.EContext ctx);

void exitE(ExprParser.EContext ctx);
...
}
   

The listener methods would have to test to see which alternative the parser
matched for each e subtree using the op token label and the methods of ctx.

因為少數(shù),剩下的多數(shù)就要進行鑒定。

省略是怎樣進行鑒定的過程(可以看書)。


然后,作者又提出了一個方法可以改善不清楚表達,從而不用鑒定;看來作者如果在中國當(dāng)官,一定沒有機會爬上去。



To get more precise listener events, ANTLR lets us label the outermost alternatives
of any rule using the # operator.



Now ANTLR generates a separate listener method for each alternative of e.
Consequently, we don’t need the op token label anymore.

作者的方法就是大學(xué)老師的土方法,點名。然后沒到的學(xué)生后面 畫個圈圈,然后。。。


For alternative label
X, ANTLR generates enterX() and exitX().

public interface LExprListener extends ParseTreeListener {

void enterMult(LExprParser.MultContext ctx);

void exitMult(LExprParser.MultContext ctx);

void enterAdd(LExprParser.AddContext ctx);

void exitAdd(LExprParser.AddContext ctx);

void enterInt(LExprParser.IntContext ctx);

void exitInt(LExprParser.IntContext ctx);
...
}


Note also that ANTLR generates specific context objects (subclasses of EContext)
for the alternatives, named after the labels.

論壇徽章:
0
33 [報告]
發(fā)表于 2014-03-11 19:05 |只看該作者
本帖最后由 oldbeginner 于 2014-03-11 19:46 編輯

******************************
7.5 Sharing Information Among Event Methods

不好意思,我們率先進入了 人工智能 信息時代
******************************




In this section, we’re going to explore mechanisms that let event methods
pass data around without altering the event method signatures.



******************************
Traversing Parse Trees with Visitors
******************************


To build a visitor-based calculator, the easiest approach is to have the event
methods associated with rule expr return subexpression values.





然后,





*********************************
Simulating Return Values with a Stack
*********************************
  

ANTLR generates listener event methods that return no values (void return
types). To return values to listener methods executing on nodes higher in the
parse tree, we can store partial results in a field of our listener.

The idea is to push the result of computing
a subexpression onto the stack. Methods for subexpressions further up the
parse tree pop operands off the stack.





Using a stack field is a bit awkward but works fine. We have to make sure
that the event methods push and pop things in the correct order across listener
events.



******************************
Annotating Parse Trees
*****************************


Instead of using temporary storage to share data between event methods, we
can store those values in the parse tree itself.

The easiest way to annotate parse-tree nodes is to use a Map that associates
arbitrary values with nodes.

得到節(jié)點數(shù)據(jù)的講解比較多,這里省略。




*************************************

選哪個?

All three are completely decoupled from the grammar itself and nicely
encapsulated in specialized objects. Beyond that, there are advantages and
disadvantages to each.



Visitor methods read nicely because they directly call other visitor methods
to get partial results and can return values like any other method.

The stack-based solution can simulate arguments and return values with a
stack, but there’s a chance of a disconnect when manually managing the
stack.

The annotator is my default solution because it allows me to arbitrarily provide
information to event methods operating on nodes above and below in the
parse tree.

恭喜 annotator 嘉賓,祝賀你 有機會和另一嘉賓 一起在 寶馬車?yán)?nbsp; 哭泣,而不是 凱越 ?怂 之流。


112.jpg (14.06 KB, 下載次數(shù): 46)

112.jpg

論壇徽章:
0
34 [報告]
發(fā)表于 2014-03-12 20:48 |只看該作者
本帖最后由 oldbeginner 于 2014-03-12 20:51 編輯

********************************
CHAPTER 8
Building Some Real Language Applications

當(dāng)下,學(xué)點實用技能吧

眼看他起高樓,眼看他宴賓客,眼看他樓塌了
********************************








深受崩潰論影響,
中國的僵尸企業(yè)正在指數(shù)級增長,失控是一瞬間的事情。

深受 成功學(xué)大師 Rick 影響,Walter就算了(在中國會被槍斃5分鐘的)。


每年3月12日 學(xué)習(xí)Rick,先從從簡單的開始,

************************
8.1 Loading CSV Data

CSV 是非常厲害 爆頭 裝備,一定要配上
************************




Our goal is to build a listener that loads comma-separated-value (CSV) data
into a nice “l(fā)ist of maps” data structure.




CSV的成分

To get precise methods within our listener, let’s label each of the alternatives
in rule field from the CSV grammar.



需要自己寫一個執(zhí)行文件,LoadCSV.java
作者分成了幾個部分來介紹 怎樣 寫 這個java文件。

感覺,作者 講解 java 的水平 和 head first java 相比 就是 小學(xué)生。

然后,




   

論壇徽章:
0
35 [報告]
發(fā)表于 2014-03-12 21:34 |只看該作者
本帖最后由 oldbeginner 于 2014-03-12 21:42 編輯

*************************
8.2 Translating JSON to XML

聊齋之,易容術(shù)
*************************



我感覺,易容術(shù) 比 windows 還要高級,對社會貢獻應(yīng)該 不相伯仲。

Lots of web services return JSON data, and we might run into a situation
where we want to feed some JSON data into existing code that accepts only
XML.

比方,原來長這樣


整整之后,
and emit XML in an equivalent form, like this:



整容過程很簡單,不知道要價為什么那么貴?

grammar JSON;

json:   object
    |   array
    ;

object
    :   '{' pair (',' pair)* '}'    # AnObject
    |   '{' '}'                     # EmptyObject
    ;
       
array
    :   '[' value (',' value)* ']'  # ArrayOfValues
    |   '[' ']'                     # EmptyArray
    ;

pair:   STRING ':' value ;

value
    :   STRING                # String
    |   NUMBER                # Atom
    |   object          # ObjectValue
    |   array                  # ArrayValue
    |   'true'                # Atom
    |   'false'                # Atom
    |   'null'                # Atom
    ;

LCURLY : '{' ;
LBRACK : '[' ;
STRING :  '"' (ESC | ~["\\])* '"' ;

fragment ESC :   '\\' (["\\/bfnrt] | UNICODE) ;
fragment UNICODE : 'u' HEX HEX HEX HEX ;
fragment HEX : [0-9a-fA-F] ;

NUMBER
    :   '-'? INT '.' INT EXP?   // 1.35, 1.35E-9, 0.3, -4.5
    |   '-'? INT EXP            // 1e10 -3e4
    |   '-'? INT                // -3, 45
    ;
fragment INT :   '0' | '1'..'9' '0'..'9'* ; // no leading zeros
fragment EXP :   [Ee] [+\-]? INT ; // \- since - means "range" inside [...]

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




同樣,要編寫
一個JSON2XML.java 文件

語法和這個java 文件,作者在書上介紹了。

然后,



這是 我志愿 為 antlr 第5版 做得 推廣 海報 (給antlr 做個廣告,希望不會被刪),

論壇徽章:
0
36 [報告]
發(fā)表于 2014-03-13 10:50 |只看該作者
****************************

小回顧,再次理解 decouple 其實 真地 非常 重要

分離 分離 再分離
***************************


我曾經(jīng)覺得應(yīng)該把 軟件專業(yè) 轉(zhuǎn)到 中文系, 現(xiàn)在 想想 覺得有點 不科學(xué),

應(yīng)該 一半 轉(zhuǎn)到 中文系,另一半 轉(zhuǎn)到 金融 系。

原因就是 軟件 很看重 decouple,尤其看設(shè)計模式 也是非常注重decouple,F(xiàn)在 中國的金融 政策 已經(jīng)基本 動彈不得,原因 是已經(jīng)完全 被 很多行業(yè) couple 了。

例如,鋼材價格 下跌 就能 讓 銀行也 產(chǎn)生 上千億的壞賬(上千億,計量單位是 100,000,000,000 元)


鐵礦石 下跌,銀行 也 中招,


一個煤老板也能讓 銀行 損失 上百億


銅價下跌,銀行 一樣 損失慘重。


等等,鋼啊、銅啊、煤啊、房啊等 每一個 都能 把 銀行 拉下 水, 銀行 就是 因為 decouple 做得不好,才被 coupled。

目前,這些損失 正在 被隱藏 ,并 被 希望 能夠 慢慢 消化 , 就像 程序員 隱藏 bug 一樣,最后 整個系統(tǒng) 崩潰。


再回顧,


一個 接 一個 行業(yè) 崩潰, 可能 只 崩潰 了一兩個,隨后 就全 蹦了,就是 因為 decouple 沒做好。


**************************

也許 崩潰 結(jié)束 后,

在廢墟 上 新成立 的銀行 ,第一條 章程 就是  decouple,

這是 血 的教訓(xùn)。






   

論壇徽章:
0
37 [報告]
發(fā)表于 2014-03-14 10:40 |只看該作者
****************************
8.3 Generating a Call Graph

我們除了他誰都不認(rèn) / 우리는 당신밖에 모른다
****************************


Software is hard to write and maintain, which is why we try to build tools to
increase our productivity and effectiveness

他 這里 指 圖片。

作者應(yīng)該是有很多生活經(jīng)驗的人,即使 放在 中國,



要不是 有 圖片,


陳金奉一邊招呼我們喝茶,一邊將他的復(fù)仇故事娓娓道來!拔疫@樣一個聰明人”常出現(xiàn)在他的言語之間,比如他一開始做紡織生意,用兩個月搞懂技術(shù),比如中學(xué)時下象棋,鮮遇敵手。提起仇家,他面露鄙夷之情,帶著勝利者的驕傲。

      “這個趙明華的生活規(guī)律已經(jīng)被我摸透了,他好色,作為法官,他好這一口兒,我就知道他死定了。他用法律整我,我就用黨的紀(jì)律整他。抓住他的把柄只是時間問題。有的有暗房,進不去,我就先不暴露自己,慢慢跟蹤。”

畫外音,真可惜,其它幾個法官 躺槍。

******************************

In this section, we’re going to build a call graph generator using the Cymbol
grammar.



語法文件 已經(jīng) 介紹過了,同時要去除 不確定,然后編寫 java 文件,

然后,


Naturally, to view the call graph, cut and paste just the output starting with
digraph G {.



With very little code, we were able to build a call graph generator in this section.

論壇徽章:
0
38 [報告]
發(fā)表于 2014-03-15 11:11 |只看該作者
*******************************
8.4 Validating Program Symbol Usage

李香蘭全局變量是日本人,所以不用死;而川島芳子全局變量是中國人,所以必須死
*******************************


1994年,周星馳主演的香港電影《國產(chǎn)凌凌漆》在劇情中提及李香蘭。片中女主角袁詠儀飾虛構(gòu)角色李香琴,其母為李香蘭,據(jù)劇情有影射文革中被打為“賣國賊”,導(dǎo)致其女香琴因此不得翻身,而不得不受金槍人的要挾。

李香蘭最受聽眾歡迎的三首歌是《何日君再來》、《蘇州夜曲》和《夜來香》。


1945年日本戰(zhàn)敗,李香蘭以漢奸罪名被逮捕,后因其日本公民身份被無罪釋放。1946年遣送回日本,1947年改回原名山口淑子繼續(xù)其演藝事業(yè)。

樓主你到底什么意圖?在敏感時期,在影射什么嗎?

日本公民 是 全局 變量, 李香蘭 是 局部變量。

其實我覺得花絮最有意思,
她曾收到了日本外交大臣松崗洋右的長子松崗謙一郎的來信。信上說:

“人的價值不能用有無名氣來衡量。人的價值并不表現(xiàn)在人的表面,你應(yīng)該珍重自己,F(xiàn)在是個人價值被愚弄的時代,你必須更加尊重自己,否則只能被國家時局?jǐn)[布。希望你永遠(yuǎn)自尊自愛!

這些話是耐人尋味的。在日本歷史最黑暗的一個時期,戰(zhàn)后被定為戰(zhàn)犯的松崗?fù)庀嘀,給一個冒充中國人(或“滿洲人”),為日本的遠(yuǎn)東政策效力的女明星寫這樣的信。


我覺得上面的這些話現(xiàn)在也可以使用,改成: 在 一個虛假 繁榮的 時代,你必須更加尊重自己,否則只能被房價、職務(wù)、炫耀擺布。希望你永遠(yuǎn)自尊自愛。
http://baike.baidu.com/subview/2 ... 45&from=rdtself

***************************

Language implementers typically call the data structure that holds symbols
a symbol table.

The language being implemented dictates a symbol table’s
structure and complexity.



The scopes form a tree.



To begin building our validator, let’s think about the big picture and form an
overall strategy.

We can break this problem down according to the key operations:
define and resolve.



To learn more about symbol table management, I shamelessly
suggest you purchase and dig through Language Implementation Patterns

If you’ve been following along pretty well so far in this section of the book,
you’re in great shape!

論壇徽章:
0
39 [報告]
發(fā)表于 2014-03-17 13:12 |只看該作者
*******************************
CHAPTER 15
Grammar Reference

跳到 最后一章 類似復(fù)習(xí)
*******************************


作者可能 編譯器水平非常高,但是 寫作水平 實在 無法恭維。

感覺這本書,重點不突出,很像流水賬。

This chapter is a reference and
summarizes grammar syntax and the key semantics of ANTLR grammars.

其實這章應(yīng)該放在 前面,而且再細(xì)致些。




******************************
15.1 Grammar Lexicon

又 詞法
*****************************



我為什么說個 又 ?

The lexicon of ANTLR is familiar to most programmers because it follows the
syntax of C and its derivatives with some extensions for grammatical
descriptions.

Comments
There are single-line, multiline, and Javadoc-style comments.




Identifiers

Token names always start with a capital letter and so do lexer rules as defined
by Java’s Character.isUpperCase() method.

Parser rule names always start with a
lowercase letter (those that fail Character.isUpperCase()).

   
Literals


All literal strings that are one or more characters in length are
enclosed in single quotes such as ';', 'if', '>=', and '\''


Actions

Actions are code blocks written in the target language.


Keywords

Here’s a list of the reserved words in ANTLR grammars: import, fragment, lexer,
parser, grammar, returns, locals, throws, catch, finally, mode, options, tokens.
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP