笑靥如花是什么意思| 平安果什么时候吃| 双规什么意思| c02是什么意思| 排骨粥要搭配什么好吃| 每天经常放屁什么原因| 一产二产三产分别包括什么| 520送男朋友什么礼物| 胆红素高吃什么食物能降得快| 冒汗是什么原因| 50而知天命什么意思| 一呼吸胸口疼是什么原因| 榆字五行属什么| 211大学是什么意思| 猪男和什么属相最配| 看脚趾头挂什么科| 香港什么时候回归| 绝经三年了突然又出血了什么原因| 派出所传唤是什么意思| 7月23日是什么日子| 干支是什么意思| 十九朵玫瑰花代表什么意思| 梅核气吃什么药最好| 宫颈病变有什么症状| 什么是邮箱地址应该怎么填写| 火车动车高铁有什么区别| 马甲线是什么| 精神小伙是什么意思| 哺乳期可以吃什么消炎药| 花儿为什么那么红| 0a是什么意思| NPY什么意思| 婴儿腹泻吃什么好| 虫草有什么功效| 小狗感冒吃什么药| 梦游是什么原因造成的| 女人吃火龙果有什么好处| 鲁迅为什么弃医从文| 榴莲吃多了有什么危害| 消化不好吃什么药| 华语是什么语言| q1什么意思| 飞机杯是什么东西| 什么的迎春花| 打葡萄糖点滴有什么用| 胃胀吃什么药最有效| 千叶豆腐是什么做的| 朵的第二笔是什么| 什么是试管婴儿| 拔了智齿需要注意什么| 单核细胞是什么| 吃什么能降胆固醇| 属鸡今年要注意什么| 痣为什么会越来越多| 三个羊念什么| 和能组什么词| 孩子不说话挂什么科| 林子大了什么鸟都有| 迁单是什么意思| 吸入物变应原筛查是什么| 拉拉裤是什么| 今夕何夕什么意思| 孕囊是什么东西| 望尘莫及是什么意思| 疗养是什么意思| 月月红是什么花| 比熊吃什么牌子狗粮好| 娭毑是什么意思| 碘酊和碘伏有什么区别| 佳木斯二院全名叫什么| 子宫穿孔有什么症状| 刘备是个什么样的人| 女性看乳房应该挂什么科| 孕妇钙片什么时间段吃最好| 湿热是什么症状| 梦见挖土豆是什么意思| 脸颊为什么会凹陷| 为什么会宫寒| 为什么会遗精| 跖疣是什么东西| 脾虚生痰吃什么中成药| 端庄是什么意思| 脸红是什么大病的前兆| 阑尾是干什么用的| 禁锢是什么意思| 鼻梁痛什么原因引起的| 为什么胸一碰就疼| fob价格是什么意思| 仓鼠可以吃什么蔬菜| 美如天仙是什么生肖| 孕妇吃什么鱼好| 小孩拉肚子应该吃什么食物好| 血小板低是什么症状| 吃香蕉有什么好处| 吃桂圆干有什么好处和坏处| 伏特加是什么意思| 阴道口痒是什么原因| 原发性肝ca什么意思| 十余年是什么意思| 命里有时终须有命里无时莫强求什么意思| 尿液有白色絮状物是什么原因| 阳字属于五行属什么| 高考移民是什么意思| 祸从口出什么意思| 什么美白效果最好最快| 失眠用什么药好| 脚上长水泡是什么原因| 女人尿多是什么原因| 消防队属于什么编制| 智能手环是干什么用的| 有什么症状| 五金是什么| 推特为什么注册不了| 千斤拔泡酒有什么功效| 坏血症什么症状| 养蛊是什么意思| 嗓子中间的那块小肉叫什么| 子宫破裂有什么危险| 召力念什么| 李时珍的皮是什么意思| 男方派去接亲要说什么| 为什么呢| 儿童发烧挂什么科| 中国科协是什么级别| spank是什么意思| 紫色是什么颜色| 出生证编号是什么| 11年属什么| 抗原体阳性是什么意思| 8朵玫瑰花代表什么意思| 咳嗽吃什么水果最好| 水泻拉肚子是什么原因| 11月份是什么星座的| 惊蛰什么意思| 什么是淋巴肿瘤| 印度人信仰什么教| 南京有什么玩的| 三生石是什么意思| 为什么不建议打卧蚕呢| 什么情况下需要做宫腔镜| 痰饮是什么意思| 老被蚊子咬是什么原因| 翕什么意思| 无与伦比是什么意思| 小孩尿酸高是什么原因| 男人人中有痣代表什么| 大姨妈来了能吃什么水果| 尿酸高有什么症状表现| 梦见牛肉有什么征兆| 勃起是什么| et是什么意思| 判缓刑是什么意思| 免疫组化检查是什么| 熟褐色是什么颜色| 侏儒症是什么原因引起的| 洁颜蜜是什么| 铁蛋白高挂什么科| 梦见自己流血是什么预兆| 卡宾男装属于什么档次| 天牛吃什么食物| 心梗吃什么药| 什么食物降血糖| 鼻炎吃什么药好| 脚后跟疼痛什么原因| 生脉饮适合什么人群| 液基薄层细胞制片术是检查什么的| 相位是什么意思| 怀孕7天有什么症状| 什么如什么什么| 犒劳是什么意思| 吃深海鱼油有什么好处和坏处| 外阴炎是什么原因引起的| 龙眼树上的臭虫叫什么| 胆囊切除后对身体有什么影响| 什么是同比| 梦见妖魔鬼怪是什么意思| 种草是什么意思| 马拉色菌是什么| 上发条是什么意思| 骨头坏死是什么感觉| 女人吃鹿茸有什么好处| 右大腿上部疼痛是什么原因| 血糖高的人吃什么主食| 角色扮演叫什么| 上梁不正下梁歪是什么意思| 吃辣椒有什么好处| 榛子是什么树的果实| 皮肤痒有什么特效药| 卯时属什么生肖| baby是什么意思| 框镜鱼是什么鱼| 过境签是什么意思| 鸡肉与什么食物相克| 劈腿什么意思| 脉搏90左右意味着什么| 蛊是什么| 6个月宝宝可以吃什么辅食| 裹小脚是什么时候开始的| 怠工是什么意思| 湿疹是什么原因| member是什么意思| ab是什么意思| 时柱比肩是什么意思| 血糖什么时候最高| 冰箱不制冷是什么问题| 脑梗是什么原因造成的| 仪仗队是什么意思| 序五行属什么| 中风是什么| 水果什么时间段吃最好| 血糖高可以吃什么蔬菜| 乙肝15阳性是什么意思| 挂绿荔枝为什么那么贵| 成人男性尿床是什么原因造成的| c5是什么驾驶证| 真菌涂片检查是查什么| 腿纹不对称有什么影响| 马克定食是什么意思| 脸上长黑痣是什么原因| 男人肾虚吃什么补得快| 小孩放臭屁是什么原因| 切尔斯什么意思| 吃避孕药对身体有什么影响| 香港五行属什么| 1985是什么年| 尿频吃什么药| 阴历六月十九是什么日子| 什么叫代谢| 古代宫刑是什么| cri是什么意思| facebook什么意思| 双下肢静脉彩超主要检查什么| 拔罐后要注意什么| 什么空调好| 血凝是什么意思| 21三体临界风险是什么意思| 4ever是什么意思| legion什么牌子| 一元硬币是什么材质| 走路快的人是什么性格| bhpc是什么牌子| 何去何从是什么意思| 西林是什么药| 成吉思汗是什么族| 69年出生属什么| 卸磨杀驴是什么意思| 倾倒是什么意思| 大型血小板比率偏低是什么意思| 脚底板黄是什么原因| rip什么意思| 食管裂孔疝是什么意思| 心跳不规律是什么原因| 打鼾挂什么科| esse是什么牌子的烟| 婴儿黄疸高有什么影响| 吃什么增加卵泡| 耳朵热是什么原因| 雨打棺材是什么征兆| 老友记是什么意思| 肝低回声结节是什么意思| 马齿苋有什么功效| 异口同声什么意思| 中医湿气重是什么意思| 鲱鱼罐头那么臭为什么还有人吃| 百度
Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

On Parsing Perl

by merlyn (Sage)
on Dec 04, 2000 at 04:21 UTC ( [id://44722]=perlmeditation: print w/replies, xml ) Need Help??

{from an alt.perl post I just made, reposted here to solicit feedback from fellow monks...}

>>>>> "Makhno" == Makhno <mak@imakhno.freeserve.co.uk> writes: Makhno> I'm thinking of writing a GUI Perl-syntax-aware editor, and Makhno> wondering what's the best way to parse perl? Highlighting Makhno> reserved words is easy (using, eg, index()) but indentifying Makhno> things like comments is a bit more difficult. Makhno> A regex like /#.*\n/ will catch comments when they are used Makhno> simply, ie: Makhno> print "hello\n"; #print hello Makhno> but will get it wrong when the '#' is used as part of a regex Makhno> (or in a string) Makhno> s#hello#goodbye#; Makhno> print "will behave like a #comment"; Makhno> Does anybody have any ideas on how I go about parsing perl Makhno> syntax in such a way, before I go to a lot of potentially Makhno> unnecessary work?
Perl is extremely difficult to parse. In fact, some would say impossible.

One thing that makes it difficult is the dual nature of a half dozen characters like "/". If that / is being used in a place that's expecting an operator, it's divide. If it's being used in a place that's expecting an operand, it's the beginning of a regular expression. So you have to keep track at all times of whether you're looking for an operator or an operand.

"No problem", you say? Quick... for the following, play the game of "regex or divide?"

sin / ... time / ... localtime / ... caller / ... eof / ...
Got those right? How about these?
use constant FOO => 35; FOO / ... use Fcntl qw(LOCK_SH); LOCK_SH / ...
OK, and now some of your own:
sub no_args (); sub one_arg ($); sub normal (@); no_args / ... one_arg / ... normal / ...
Got those too? How about these (same problem, different file):
use Random::Module qw(aaa bbb ccc); aaa / ... bbb / ... ccc / ...
A little harder, eh? So now you have to parse OUTSIDE the file to get your answer. And as if that wasn't enough, let's get weird:
BEGIN { eval (time % 2 ? 'sub zany ();' : 'sub zany (@);'); } zany / ...
Quick, was that last one a divide or a regex start?

Why does it matter? Look at this:

sin / 25 ; # / ; die "this dies!"; time / 25 ; # / ; die "this doesn't die";
The first one is computing the sin of the true/false value gotten by matching " 25 ; # " against $_. Then it dies. The second one is computing the time of day divided by 25, then ignoring the comment.

Starting to see the trouble?

This leads people to say "the only thing which can parse Perl (the language) is perl (the binary)". Maybe not for Perl6. But for the Perl we know and can use today, certainly so.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: On Parsing Perl
by quidity (Pilgrim) on Dec 04, 2000 at 04:40 UTC

    I do most of my perl coding using CPerl mode for Xemacs, and although it is very good at spotting syntax it is often horribly wrong, especially when odd quoting characters or pod is brought into the equation. eval is even worse. I'd advise anyone even thinking of trying to parse perl to look at what can be achieved, and then either improving that (to the benefit of everyone) or just to give up.

    I do sometimes find myself chosing a particular way of coding over another (possibly better) way because the second breaks the pretty printing, and I want others using the same editor to be able to read the code I write.

      I hate to say this, but even as an XEmacs fan, cperl+gemacs is far superior. Recent GNU Emacs has some extra stuff that XEmacs doesn't have, that allows cperl to do some really amazing things. (I have vague memories of the "extra stuff" being multiple syntax transition tables for each character, so you can gracefully handle things like m!!x and other non-standard delimiters. But I could be totally wrong.)
Re: On Parsing Perl
by repson (Chaplain) on Dec 04, 2000 at 07:28 UTC
    You could use B::Deparse for some of it, which eliminates some of what you don't want, but even that fails on many other things. The best bet is to code for the majority of perl and leave programmers to use their heads for the rest. This is what I do with syntax highlighting in vim, I use it generally but don't belive it for a moment. It is still sometimes helpful anyway. This is the way it will have to stay for now, at least until Perl6...
Re: On Parsing Perl
by toadi (Chaplain) on Dec 04, 2000 at 13:49 UTC
    I'm with you merlyn. I use vim(*nix) and textpad(windows), both make some mistakes. Like in some regex syntax it does some weird things...


    --
    My opinions may have changed,
    but not the fact that I am right

Re: On Parsing Perl
by nop (Hermit) on Dec 04, 2000 at 21:21 UTC
    I use the perl mode on emacs, and resort to small tricks to keep everything ok. For example,
    s/'"/;
    upsets the syntax colorization badly (as emacs thinks following code is in the string), so I use idioms like
    s/'"/; #"'
    to "close" my "open" strings....
Eight years later...
by samwyse (Scribe) on Jan 13, 2009 at 19:08 UTC
    I decided to run this test script under various versions of Perl.
    @examples = split /\n/, <<'EXAMPLES'; sin / ... time / ... localtime / ... caller / ... eof / ... use constant FOO => 35; FOO / ... use Fcntl qw(LOCK_SH); LOCK_SH / ... sub no_args (); sub no_args{1}; no_args / ... sub one_arg ($); sub one_arg{1}; one_arg / ... sub normal (@); sub normal{1}; normal / ... EXAMPLES for (@examples) { s=\.\.\.=25 ; # / ; die "this dies!";=; local($a) = eval; $a = $@ if $@; print "$_\n\t$a\n"; }
    I don't know what the results would be for earlier versions, but from Perl 5.6 onwards it's pretty consistent.
    Example5.0065.0085.010
    sindiesdiesdies
    time49274891.7249274891.7449274891.76
    localtimediesdiesdies
    callerdiesdiesdies
    eofdiesdiesdies
    FOO1.41.41.4
    LOCK_SHdiesdies0.04
    no_argsdiesdiesdies
    one_argdiesdiesdies
    normaldiesdiesdies
    Most of the "dies" instances also produced this message: Warning: Use of "XXX" without parentheses is ambiguous at (eval N) line 1. However, the LOCK_SH example never generated errors, while the last three generated "Prototype mismatch" messages. I must also note that almost all of the examples generated warnings, despite using neither the '-w' option or 'use strict;'
Re: On Parsing Perl (Once upon a time)
by Anonymous Monk on Jul 11, 2022 at 11:27 UTC
    I'm currently working on something, (basically perl parser), and apart from the BEGIN block, everything seems parseable (is that the right word?) using some simple LL grammars. Or am I just too uneducated?
      everything seems parseable (is that the right word?) using some simple LL grammars.

      No, only perl (the interpreter) can parse all of Perl (the language). See my node here for details.

      Edit: added emphasis.

      Static parsing is only reliable, if you rule out or control all imported subs, because prototypes change the way Perl is parsed. See HaukeX's other reply.

      Basically changes at compile time ( see BEGIN blocks ) can change the parser.

      Dynamic parsing is possible though, if you inspect the op-tree after compilation, that's the basic idea of some newer tools, like the perlnavigator.

      See also perl -c in perlrun or B::Xref

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Static parsing is only reliable, if you rule out or control all imported subs, because prototypes change the way Perl is parsed.

        I think, though, that prototypes aren't the only reason Perl isn't statically parseable. There are quite a few heuristics that the parser uses that aren't all too well documented, and I'm not sure if a static parser would be able to reimplement all of them. And then there is no strict code, which I think gets even trickier. At some point I was considering researching and making a list of all of the reasons, but I unfortunately never got around to it.

Re: On Parsing Perl
by gaggio (Friar) on Dec 04, 2000 at 04:35 UTC
    I don't know if I am with you there. What is the Perl executable doing when it executes a script? Isn't that called parsing also?
      Stunning reading comprehension there. I wonder if such clueless folk ever come back and read their comments years later and feel a twinge of embarrassment.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://44722]
Approved by root
Front-paged by htoug
help
Chatterbox?
and all is quiet...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2025-08-05 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.


    异烟肼是什么药 26度穿什么衣服 呕心沥血是什么意思 哪吒妈妈叫什么 汕是什么意思
    荼蘼是什么意思 柱镜度数是什么意思 老鼠爱吃什么食物 皮肤经常痒是什么原因 扁平疣是什么样子图片
    花花世界不必当真是什么歌 75是什么意思 垂体泌乳素高是什么原因 2003年属羊的是什么命 腿毛多是什么原因
    一九六三年属什么生肖 政委是干什么的 11月1日什么星座 4月28号是什么星座 榄仁是什么
    梦见大蜈蚣是什么预兆hcv8jop1ns0r.cn 255是什么意思520myf.com 卤水是什么水hcv9jop5ns2r.cn 大象灰是什么颜色hcv9jop6ns0r.cn 生孩子大出血是什么原因造成的hcv9jop5ns0r.cn
    hc是胎儿的什么hcv8jop1ns6r.cn 咽喉炎吃什么药能治好hcv8jop1ns8r.cn 霍山黄芽属于什么茶hcv8jop8ns6r.cn 89年蛇是什么命hcv9jop0ns3r.cn 妈妈的弟弟的老婆叫什么imcecn.com
    盐袋子热敷有什么好处hcv9jop1ns9r.cn 718是什么星座jingluanji.com 怀孕吃什么宝宝会白hcv8jop5ns8r.cn 年轻人心悸是什么原因hcv8jop1ns2r.cn 白里透红的透是什么意思hcv8jop8ns8r.cn
    气垫是什么hcv9jop5ns5r.cn 吃什么能增强免疫力和抵抗力hcv8jop7ns2r.cn 嘴唇起泡是什么原因引起的hcv9jop0ns5r.cn 什么的鼻子填词形容词hanqikai.com 弊端是什么意思xjhesheng.com
    百度