甲状腺结节是什么病| 男士内裤买什么牌子好| 水过鸭背是什么意思| 金银花有什么效果| 腔梗和脑梗有什么区别| 为什么要补钾| 龙脉是什么意思| 花金龟吃什么| 69是什么意思| 人彘为什么还能活着| 手指疣初期什么样子| cvt是什么意思| qn医学上是什么意思| rov是什么意思| 3月29日是什么星座| 跑完步喝什么水最好| 脾虚湿热吃什么中成药| 亨廷顿舞蹈症是什么病| 梦见老鼠是什么预兆| 铁瓷是什么意思| 得之坦然失之淡然是什么意思| 牛黄安宫丸什么季节吃| 小孩办理护照需要什么材料| 英气是什么意思| 脚臭用什么泡脚效果好| 焦虑是什么| hrp是什么意思| 宫颈肥大伴纳氏囊肿是什么意思| 办健康证需要带什么证件| 孕吐严重是什么原因| 为什么手指关节会痛| 转氨酶是什么| 老人流口水是什么原因引起的| 唐氏综合症是什么原因| 21度穿什么衣服| 血糖高是什么意思| 宫内囊性回声代表什么| 马上风是什么意思| 头皮痒用什么药最有效| 什么而起| 梦见房子漏水是什么意思| 白兰地兑什么饮料好喝| 人为什么会生气| 楚楚动人是什么意思| 头皮痒是什么原因引起的| 眼皮红肿是什么原因| 什么是命运| 经常出鼻血是什么原因| 黑玫瑰代表什么| 做什么运动可以长高| 心律不齐是什么病| 咳嗽喝什么茶| gfr医学上是什么意思| 遮挡车牌属于什么行为| 梦到鸡是什么意思| 吹箫什么意思| 月经来头疼是什么原因引起的| 金为什么克木| as是什么元素| 掰弯了是什么意思| 咽喉疼吃什么药| emmm什么意思| gg是什么品牌| 身上长黑痣是什么原因| 亚健康是什么意思| 调和油是什么油| 建卡需要带什么证件| 老年人吃什么| 四川九寨沟什么时候去最好| 三百年前是什么朝代| 荼靡是什么意思| 总胆汁酸高是什么意思| 牙齿贴面是什么意思| dm什么意思| 晚上吃什么水果减肥效果最好| 吃酒酿有什么好处| 世态炎凉是什么意思| 1985年海中金命缺什么| 姐妹是什么生肖| 拆封是什么意思| 知了什么时候叫| 港式按摩是什么意思| 备孕需要注意些什么| 危如累卵是什么意思| 梦见大蛇是什么意思| 姜枣茶什么季节喝最好| generic是什么意思| 老是掉头发是什么原因| 看见蜈蚣有什么预兆| 闭合性跌打损伤是什么意思| 单核细胞百分比偏高是什么意思| 西天取经是什么意思| 狗狗胰腺炎有什么症状| 兔子可以吃什么| 破损是什么意思| 吃什么补蛋白质最快| 2019属什么生肖| 学业有成是什么意思| 嗓子疼吃什么药| c3是什么车型| 无花果是什么季节的水果| 肾虚对男生意味着什么| 中风吃什么药最有效| 高血压是什么| 脑供血不足吃点什么药| 精尽人亡是什么意思| 米放什么不生虫子| 2029是什么年| 12月22号是什么星座| 当归炖鸡有什么功效| 睾丸积液是什么原因造成的| 青光眼是什么| 每天一杯蜂蜜水有什么好处| 都有什么瓜| 见血是什么兆头| 沥水是什么意思| 文化大革命什么时候| 手掌横纹代表什么意思| 3月26号是什么星座| 什么药治鼻炎| 工作是什么意思| 喝啤酒有什么好处| 爱新觉罗是什么旗| 右冠优势型是什么意思| 王字旁的字跟什么有关| 家里有小蜘蛛预示什么| 过敏性皮炎吃什么药好| 维u是什么药| 乙肝阳性是什么意思| 粟是什么| 通五行属什么| 2005年属什么生肖| 光天化日什么意思| 新婚志喜是什么意思| 胎盘低置是什么原因造成的| 眼屎多什么原因| 摄影三要素是什么| 人工牛黄是什么| 口巴念什么| 大名鼎鼎的鼎是什么意思| gbs检查是什么| 负数是什么意思| pi什么意思| 耳朵外面痒是什么原因| 抵抗力差是什么原因| 匝道什么意思| 罗嘉良为什么娶苏岩| 崎字五行属什么| 为什么会有湿气| 茄子和什么不能一起吃| 从胃到小腹连着疼是什么原因| 查血糖是什么检查项目| 福星贵人是什么意思| 12月24号是什么星座| 宝宝什么时候添加辅食最好| 狐臭和腋臭有什么区别| 藕粉色是什么颜色| 铁皮石斛能治什么病| 忙什么呢幽默的回答| 天干指的是什么| 疯狂动物城狐狸叫什么| 孕妇缺铁吃什么| 鸭子炖汤和什么一起炖最有营养| 退行性病变是什么意思| 毛宁和毛阿敏是什么关系| 月经过后腰酸疼是什么原因| 什么是单克隆抗体| 属马的本命佛是什么佛| 胡子为什么长得快| 白泽是什么| 肚子绞痛吃什么药| 背德感是什么意思| 天上的月亮是什么生肖| 主心骨是什么意思| 10月20日什么星座| 指南针是什么时候发明的| 肌肉损伤吃什么药| 赵云的坐骑是什么马| 一个口一个巴念什么字| pac是什么| 什么食物降血糖| veromoda是什么牌子| 乌龟吃什么蔬菜| 身上湿气重吃什么药| 子宫前置是什么意思| 为什么男人喜欢女人的胸| 省人大代表是什么级别| 67年的羊是什么命| 中考送什么礼物| 办理社保卡需要什么资料| 身份证是什么字体| 狗吃什么蔬菜好| 卓玛什么意思| 什么也什么造句| 代谢不好吃什么药| 鹅肉不能和什么一起吃| 芸豆是什么| 牙龈肿痛吃什么药好得快| 早射吃什么药| 一龙一什么填十二生肖| 打新股需要什么条件| 为什么要闰月| 脖子上长扁平疣是什么原因| 早上起来口苦是什么原因| 750是什么金| 鸭胗是鸭的什么部位| 镭射有什么危害| 金秘书为什么那样| 八月六号是什么星座| 吗啡是什么药| 农历十二月是什么月| 咽炎吃什么好| 晚上吃什么减肥效果最好| 鱼吃什么食物| 老是低血糖是什么原因| 自由行是什么意思| 闰月要给父母买什么| 吃什么可以软化血管| 小腿有血栓是什么症状| 什么不得| 羊水穿刺是查什么的| gris是什么颜色| 梗米是什么| 紧急避孕药对身体有什么伤害| 肺部钙化是什么意思啊| 喝什么对肾好| 做梦梦见蛇是什么征兆| 迟钝是什么意思| 身份证借给别人有什么危害性| 什么是结缔组织病| 十月初一是什么节| 隐睾是什么意思| 胃下垂吃什么药最好| 高烧拉肚子是什么原因| 心脏病吃什么好| 猫为什么不怕蛇| 梦到自己快要死了是什么意思| 哮喘咳嗽吃什么药好得快| 内推是什么意思| 沙茶是什么| 以什么乱什么| 女性得了性病有什么症状| 胆汁反流是什么症状| 钙化灶什么意思| 智齿有什么用| 梦见已故的老人是什么意思| 减肥晚餐适合吃什么| 风波是什么意思| 快递属于什么行业| 月经来了腰疼是什么原因| 19点是什么时辰| 白酒泡什么补肾壮阳最好| 梦见飞机是什么意思| 全血铅测定是什么意思| 良知是什么意思| 2026年是什么生肖年| 什么叫高血压| 胃酸的主要成分是什么| 精索静脉曲张是什么原因导致的| 华胥是什么意思| 被强奸是什么感觉| 穿匡威的都是什么人| 碘化银什么颜色| 轩字属于五行属什么| 百度
Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 browsing the Monastery: (4)
As of 2025-08-08 22:09 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.


    乳头痒是怎么回事是什么原因 吃什么补内膜最快 口干舌燥挂什么科 腰椎间盘突出挂什么科室 柠檬有什么功效和作用
    刷酸是什么 otg线是什么 南海龙王叫什么 云为什么是白色的 为什么要备孕
    无头鱼是什么鱼 月亮的肚子指的是什么 女性体毛多是什么原因 西洋菜是什么菜 维生素c是什么
    拔牙后注意什么 非经期出血是什么原因 自古红颜多薄命是什么意思 狗可以吃什么水果 华为什么手机好
    处女和什么座最配对hcv9jop6ns5r.cn 血小板分布宽度偏低是什么意思hcv8jop0ns5r.cn gsy什么意思zsyouku.com 舅舅的儿子叫什么hcv8jop9ns9r.cn 羊悬筋是什么样子图片hcv9jop5ns4r.cn
    血小板低会出现什么症状onlinewuye.com 七月七日是什么生肖520myf.com 24岁属什么hcv8jop7ns5r.cn 什么什么闻名hcv8jop2ns9r.cn 梦见小麦粒是什么意思hcv9jop3ns5r.cn
    手机什么时候发明的hcv8jop0ns9r.cn 舅子是什么意思hcv7jop7ns4r.cn 屁臭是什么原因hcv9jop1ns2r.cn 鼻咽部淋巴组织增生是什么意思hcv9jop1ns9r.cn cp什么意思网络用语hcv8jop0ns5r.cn
    牙龈紫色是什么原因hcv9jop5ns6r.cn 什么是双相情感障碍hcv8jop9ns4r.cn 什么人不能喝绿豆汤hcv7jop9ns9r.cn 99年属什么hcv9jop5ns1r.cn today什么意思hcv7jop9ns0r.cn
    百度